Skip to content
This repository

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

Open
wants to merge 1 commit into from
Sean 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.

Matthew Daubert

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

Steve Schwartz
Collaborator

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

Antares Meketa
ameketa commented May 01, 2013

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

Andrei Erdoss

+1

Nikolay Shebanov

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

Steve Schwartz
Collaborator

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

Steve Schwartz
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?

Sean Santry
santry commented May 29, 2013

@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.

Joe Ferris jferris referenced this pull request in thoughtbot/capybara-webkit July 17, 2013
Open

AJAX POST content-type Warning #260

elangolab

+1

+1

George Rafi Bonvanie

+1

Esteban Arango Medina

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

Showing 1 unique commit by 1 author.

Jan 27, 2013
Sean Santry set request Content-Type for data-remote links d8724ea
This page is out of date. Refresh to see the latest.
4  src/rails.js
@@ -115,6 +115,10 @@
115 115
           type: method || 'GET', data: data, dataType: dataType,
116 116
           // stopping the "ajax:beforeSend" event will cancel the ajax request
117 117
           beforeSend: function(xhr, settings) {
  118
+            if (settings.type == 'POST') {
  119
+              xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
  120
+            }
  121
+
118 122
             if (settings.dataType === undefined) {
119 123
               xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
120 124
             }
15  test/public/test/data-remote.js
@@ -78,6 +78,21 @@ asyncTest('clicking on a link with data-remote attribute', 5, function() {
78 78
     .trigger('click');
79 79
 });
80 80
 
  81
+asyncTest('clicking on a link with data-remote attribute and data-method post', 6, function() {
  82
+  $('a[data-remote]')
  83
+    .attr('data-method', 'post')
  84
+    .bind('ajax:success', function(e, data, status, xhr) {
  85
+      App.assert_callback_invoked('ajax:success');
  86
+      App.assert_request_path(data, '/echo');
  87
+      equal(data.CONTENT_TYPE, 'application/x-www-form-urlencoded; charset=UTF-8', 'Content-Type header should be set');
  88
+      equal(data.params.data1, 'value1', 'ajax arguments should have key data1 with right value');
  89
+      equal(data.params.data2, 'value2', 'ajax arguments should have key data2 with right value');
  90
+      App.assert_post_request(data);
  91
+    })
  92
+    .bind('ajax:complete', function() { start() })
  93
+    .trigger('click');
  94
+});
  95
+
81 96
 asyncTest('changing a select option with data-remote attribute', 5, function() {
82 97
   $('form')
83 98
     .append(
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.