Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
adding global $.restSetup object to change csrf token, method parameter
  • Loading branch information
Adam Lassek committed Sep 13, 2010
1 parent 3022a79 commit 7c18cf7
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions jquery.rest.js
Expand Up @@ -22,6 +22,13 @@

(function($){

// Change the values of this global object if your csrf token or method
// parameter are different. You can set csrf to null to skip it (but you shouldn't)
$.restSetup = {
csrf: { authenticity_token: null },
methodParam: '_method'
};

// jQuery doesn't provide a better way of intercepting the ajax settings object
var _ajax = $.ajax,
options = { dataType: 'json' };
Expand Down Expand Up @@ -77,14 +84,14 @@

settings.data = settings.data || "";

if ("AUTH_TOKEN" in window)
if ($.restSetup.csrf && !$.isEmptyObject($.restSetup.csrf))
if (!/^(get)$/i.test(settings.type))
if (!/(authenticity_token=)/i.test(settings.data)) {
settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
settings.data += (settings.data ? "&" : "") + $.param($.restSetup.csrf);
}

if (!/^(get|post)$/i.test(settings.type)) {
settings.data += (settings.data ? "&" : "") + "_method=" + settings.type.toLowerCase();
settings.data += (settings.data ? "&" : "") + $.restSetup.methodParam + '=' + settings.type.toLowerCase();
settings.type = "POST";
}

Expand Down

0 comments on commit 7c18cf7

Please sign in to comment.