Skip to content
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ See defaults [here](https://github.com/jpillora/jquery.rest/blob/gh-pages/src/jq

A number reprenting the number of seconds to used previously cached requests. When set to `0`, no requests are stored.

#### csrf

Sets a CSRF token that will be sent with every request via the X-CSRFToken header.

### cachableTypes

An array of strings reprenting the HTTP method types that can be cached. Is `["GET"]` by default.
Expand Down Expand Up @@ -486,7 +490,6 @@ Since each Resource can have it's own set of options, at instantiation time, opt

Todo
---
* CSRF
* Add Tests

Contributing
Expand Down
4 changes: 4 additions & 0 deletions dist/1/jquery.rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ deleteWarning = function() {
defaultOpts = {
url: '',
cache: 0,
csrf: '',
request: function(resource, options) {
return $.ajax(options);
},
Expand Down Expand Up @@ -350,6 +351,9 @@ Resource = (function() {
headers['X-HTTP-Method-Override'] = method;
method = 'POST';
}
if (this.opts.csrf) {
headers['X-CSRFToken'] = this.opts.csrf;
}
if (this.opts.stripTrailingSlash) {
url = url.replace(/\/$/, "");
}
Expand Down
2 changes: 1 addition & 1 deletion dist/1/jquery.rest.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/jquery.rest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ deleteWarning = ->
defaultOpts =
url: ''
cache: 0
csrf: ''
request: (resource, options) -> $.ajax(options)
isSingle: false
autoClearCache: true
Expand Down Expand Up @@ -112,7 +113,6 @@ class Verb

#resource class - represents one set of crud ops
class Resource

constructor: (nameOrUrl, options = {}, parent) ->
validateOpts options
if parent and parent instanceof Resource
Expand Down Expand Up @@ -236,6 +236,9 @@ class Resource
headers['X-HTTP-Method-Override'] = method
method = 'POST'

if @opts.csrf
headers['X-CSRFToken'] = @opts.csrf

if @opts.stripTrailingSlash
url = url.replace /\/$/, ""

Expand Down