From 46c0ad293312de44af3262a0c3dfa2e9eb4b70d3 Mon Sep 17 00:00:00 2001
From: raggi
Date: Fri, 18 Mar 2011 14:01:39 -0700
Subject: [PATCH] Some browsers have poor support for non-GET/POST xhr, and
will cause 411's on non-DoS-able servers. Submitting a using fallbacks
protects against this. Also ensures CSRF protection for PUT links.
---
src/rails.js | 42 +++++++++++++++++++++++-------------------
1 file changed, 23 insertions(+), 19 deletions(-)
diff --git a/src/rails.js b/src/rails.js
index 5f14c5ae..2c8658a6 100644
--- a/src/rails.js
+++ b/src/rails.js
@@ -21,13 +21,31 @@
return event.result !== false;
}
+ function buildMethodForm(link) {
+ var href = link.attr('href'),
+ method = link.data('method'),
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
+ form = $(''),
+ metadata_input = '';
+
+ if (csrf_param !== undefined && csrf_token !== undefined) {
+ metadata_input += '';
+ }
+
+ form.hide().append(metadata_input);
+ return form;
+ }
+
// Submits "remote" forms and links with ajax
function handleRemote(element) {
var method, url, data,
dataType = element.data('type') || ($.ajaxSettings && $.ajaxSettings.dataType);
- if (fire(element, 'ajax:before')) {
- if (element.is('form')) {
+ if (fire(element, 'ajax:before')) {
+ if (!element.is('form'))
+ element = buildMethodForm(element);
+
method = element.attr('method');
url = element.attr('action');
data = element.serializeArray();
@@ -37,11 +55,7 @@
data.push(button);
element.data('ujs:submit-button', null);
}
- } else {
- method = element.data('method');
- url = element.attr('href');
- data = null;
- }
+
$.ajax({
url: url, type: method || 'GET', data: data, dataType: dataType,
// stopping the "ajax:beforeSend" event will cancel the ajax request
@@ -67,18 +81,8 @@
// Handles "data-method" on links such as:
// Delete
function handleMethod(link) {
- var href = link.attr('href'),
- method = link.data('method'),
- csrf_token = $('meta[name=csrf-token]').attr('content'),
- csrf_param = $('meta[name=csrf-param]').attr('content'),
- form = $(''),
- metadata_input = '';
-
- if (csrf_param !== undefined && csrf_token !== undefined) {
- metadata_input += '';
- }
-
- form.hide().append(metadata_input).appendTo('body');
+ var form = buildMethodForm(link);
+ form.appendTo('body');
form.submit();
}