question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot override data in ajax:beforeSend Event

See original GitHub issue

I try to send some data with a post ajax request like this and in the server side i dont receive the data (but i do see the value in firebug)

$('a[data-with], a[data-submit]').live('ajax:beforeSend', function(event, xhr, settings) {
      settings.data = "name=John&time=2pm";
});

but if im doing like this it s working

$('a[data-with], a[data-submit]').live('ajax:beforeSend', function(event, xhr, settings) {
      settings.url += "?name=John&time=2pm";
});

My route is configured as a POST method

resources :categories do
      collection do 
        post :addcategory
      end
    end

What im doing wrong ?

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
regis-leraycommented, May 29, 2011

Oki im pretty satisfied with the solution to use the ajax:before like i did.

But i m still convinced there is an issue with this line in jquery-ujs.js(line 115)

data = element.data('params') || null; data

and we need at on point to clean the data store in the current element by doing

element.removeData('params')

if not if you change the data after the first submission, it will continue to send the same value.

to get working this is what i was doing

//clean the attribute, dont need anymore
    $('a[data-with], a[data-submit]').live('ajax:complete', function(event, xhr, status) {
      var el = $(this);
      el.removeData('params');
    });
0reactions
ramontayagcommented, Sep 30, 2011

Hey @JangoSteve, I tried to do this with forms, but after reading the code a little bit more closely, it seems that data-params was only for rails.inputChangeSelector and non-forms. Did I read it right?

How would you go about appending data to a form before sending it out? I have one already in a long serialized string that I get from another plugin, and would simply like to append it to the data of the form.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

using ajax beforeSend to modify data - Stack Overflow
As you can see, the setting processData should to be false. So it can be setted when you do request in $.ajax() or...
Read more >
jQuery.ajax() | jQuery API Documentation
The jqXHR and settings objects are passed as arguments. This is an Ajax Event. Returning false in the beforeSend function will cancel the...
Read more >
Working with JavaScript in Rails - Ruby on Rails Guides
This guide covers the built-in Ajax/JavaScript functionality of Rails (and more); it will enable you to create rich and dynamic Ajax applications with...
Read more >
Ajax/jQuery.getJSON Simple Example - SitePoint
Learn how to use jQuery's getJSON helper to load JSON-encoded data from a server using a GET HTTP request.
Read more >
JQueryAjaxSettings | typescript - v3.7.7
ajax (), then it is always sent to the server (even if no data is sent). As of jQuery 1.6 you can pass...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found