Skip to content

Instantly share code, notes, and snippets.

@nozzlegear
Created December 17, 2014 02:20
Show Gist options
  • Save nozzlegear/4abc71e8f9731f37c68e to your computer and use it in GitHub Desktop.
Save nozzlegear/4abc71e8f9731f37c68e to your computer and use it in GitHub Desktop.
Extending jQuery to send json in a post with an ASP.NET AntiForgeryToken header
jQuery.postJSON = function (url, data, success, antiForgeryToken, dataType) {
if (dataType === void 0) { dataType = "json"; }
if (typeof (data) === "object") { data = JSON.stringify(data);}
var ajax = {
url: url,
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: dataType,
data: data,
success: success
};
if (antiForgeryToken) {
ajax.headers = {
"__RequestVerificationToken": antiForgeryToken
};
};
return jQuery.ajax(ajax);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment