$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
带X-CSRFToken请求:
$.ajax({
url: "/cookie_ajax/",
type: "POST",
// 从Cookie取csrftoken, 并设置到请求头中
headers: {"X-CSRFToken": $.cookie("csrftoken")},
data: {
"username": "yang",
"password": 123
},
success: function(data){
console.log(data)
}
});
自己写一个getCookie方法:
function getCookie(name){
var cookieValue = null;
if (document.cookie && document.cookie !== ""){
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++){
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + "=")){
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
var csrftoken = getCookie("csrftoken");