Skip to content

Instantly share code, notes, and snippets.

@bestpika
Last active February 3, 2017 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bestpika/07424518dde4aef1b68707cca8669e87 to your computer and use it in GitHub Desktop.
Save bestpika/07424518dde4aef1b68707cca8669e87 to your computer and use it in GitHub Desktop.
在 ajax 送出 post 前加上 token #mvc #ajax
$.ajaxPrefilter((o) => {
if (o.type.toLowerCase() == 'post' && o.contentType.toLowerCase().includes('x-www-form-urlencoded')) {
var d = o.data.split('&'); // data
var i = $('input[name="__RequestVerificationToken"]:first'); // one
if (i.length > 0) {
var n = i.attr('name');
var v = encodeURIComponent(i.val());
var k = n + '=' + v;
d.push(k);
o.data = d.join('&');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment