Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

set request Content-Type for data-remote links #301

Open
wants to merge 1 commit into from
@santry

This pull request sets the Content-Type header to 'application/x-www-form-urlencoded; charset=UTF-8' for Ajax requests generated for links with data-remote="true" and data-method="post".

Without this patch, no Content-Type header is included in the requests that jquery-ujs generates for these links. Tools such as capybara-webkit issue repeated warnings about these missing headers and these warnings clutter the display of test results (see thoughtbot/capybara-webkit#260).

I've added an appropriate test that passes on Chrome, Safari, Firefox and IE 7-10.

@MDaubs

We are also seeing this issue and have resorted to patching our applications. This seems like something that should definitely be merged upstream.

@JangoSteve
Collaborator

Good deal. Thanks for the test! I'll pull this in, and release a new jquery-rails gem soon.

@santry santry referenced this pull request in thoughtbot/capybara-webkit
Closed

Getting a "QFont::setPixelSize: Pixel size <= 0 (0)" Warning Message #485

@ameketa

:+1: this would be very helpful to get out in a new release.

@killthekitten

+1, this annoying stuff should be fixed :+1: /cc @JangoSteve how's it going?

@JangoSteve
Collaborator

I can't get this test to fail in the old version of jquery-ujs. I think I may be missing something.

@JangoSteve
Collaborator

I deleted the change to rails.js and just kept the test, ran it in Chrome, Firefox, Safari, and IE9, and the test passed on all. What browser were you running it on where it failed?

@santry

@JangoSteve the failure happens in capybara-webkit. I didn't want to introduce that dependency into the project so I didn't include it with this pull request.

@jferris jferris referenced this pull request in thoughtbot/capybara-webkit
Closed

AJAX POST content-type Warning #260

@rono23

+1

@esbanarango

what's the status on this? will it be merged?...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 28, 2013
  1. @santry
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 0 deletions.
  1. +4 −0 src/rails.js
  2. +15 −0 test/public/test/data-remote.js
View
4 src/rails.js
@@ -115,6 +115,10 @@
type: method || 'GET', data: data, dataType: dataType,
// stopping the "ajax:beforeSend" event will cancel the ajax request
beforeSend: function(xhr, settings) {
+ if (settings.type == 'POST') {
+ xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
+ }
+
if (settings.dataType === undefined) {
xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
}
View
15 test/public/test/data-remote.js
@@ -78,6 +78,21 @@ asyncTest('clicking on a link with data-remote attribute', 5, function() {
.trigger('click');
});
+asyncTest('clicking on a link with data-remote attribute and data-method post', 6, function() {
+ $('a[data-remote]')
+ .attr('data-method', 'post')
+ .bind('ajax:success', function(e, data, status, xhr) {
+ App.assert_callback_invoked('ajax:success');
+ App.assert_request_path(data, '/echo');
+ equal(data.CONTENT_TYPE, 'application/x-www-form-urlencoded; charset=UTF-8', 'Content-Type header should be set');
+ equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value');
+ equal(data.params.data2, 'value2', 'ajax arguments should have key data2 with right value');
+ App.assert_post_request(data);
+ })
+ .bind('ajax:complete', function() { start() })
+ .trigger('click');
+});
+
asyncTest('changing a select option with data-remote attribute', 5, function() {
$('form')
.append(
Something went wrong with that request. Please try again.