Skip to content

Add correct Content-Type when ajax:beforeSend sets data.#119

Closed
Confiscative wants to merge 1 commit intorails:masterfrom
Confiscative:master
Closed

Add correct Content-Type when ajax:beforeSend sets data.#119
Confiscative wants to merge 1 commit intorails:masterfrom
Confiscative:master

Conversation

@Confiscative
Copy link

This modification allows to unobtrusively add data to remote POST or PUT tags that aren't forms via the ajax:beforeSend event. Normally when data is provided the Content-Type is set internally by jQuery, but this happens before the beforeSend callback has been handled (line 6707, jQuery v1.5.1).

Example:

link_to("Flag", foo_path, :remote => true, :method => :put, :class => :do_foo)

$( function() {
    $(".do_foo").bind("ajax:beforeSend", function(event, xhr, settings) {
        settings.data = jquery.param({"foo[bar]": magic_value});
    });
});

…Type if data is provided in the 'ajax:beforeSend' event.
@neerajsingh0101
Copy link

With this commit 5b23c3d now you have a before callback which is fired before the beforeSend is activated.

I think this might help you tackle your problem in a nicer way.

Do you still think this pull request should be merged?

@Confiscative
Copy link
Author

The 'before'-fix you proposed seems like a better solution indeed. I'll close this one down then.

@jpalermo
Copy link

jpalermo commented Apr 7, 2011

The ajax:before solution still only works if you have a form. If you are using a link to generate an ajax request and want to attach data to the request before it goes out, there is no clean way of doing it. If you do it in ajax:beforeSend, you then have to manually set the Content-Type header for the xhr request.

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants