Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/rails.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions test/public/test/data-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down