我正在尝试做一个基本的帖子,我得到了一个“低级案例”'错误。
我的网址是"开发者/ 58ace2b191e01c24f61fc241 / broken" 并且请求是" http://localhost:8080/developer/58ace2b191e01c24f61fc241/broke",但由于某种原因,这不起作用。
broke = function (url) {
var request = getRootUrl() + url;
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$(document).ajaxSend(function(e, xhr, options) {
xhr.setRequestHeader(header, token);
});
$.post(request, function(data) {
console.log("hit");
});
};
但是,我有一个非常相似的删除方法可以工作:
makeDeleteCall = function (url) {
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
$(document).ajaxSend(function(e, xhr, options) {
xhr.setRequestHeader(header, token);
});
$.ajax({
type: "DELETE",
url: getRootUrl() + url
});
};
完整堆栈跟踪:
Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
at Object.setRequestHeader (http://localhost:8080/js/jquery.js:4:19436)
at HTMLDocument.<anonymous> (http://localhost:8080/js/developer.js:22:13)
at HTMLDocument.dispatch (http://localhost:8080/js/jquery.js:3:8436)
at HTMLDocument.m.event.add.r.handle (http://localhost:8080/js/jquery.js:3:5139)
at Object.trigger (http://localhost:8080/js/jquery.js:3:7537)
at Function.ajax (http://localhost:8080/js/jquery.js:4:21187)
at Function.m.each.m.(anonymous function) [as post] (http://localhost:8080/js/jquery.js:4:22297)
at broke (http://localhost:8080/js/developer.js:24:7)
at HTMLTableCellElement.onclick (http://localhost:8080/newDev:58:136)
那么,为什么这适用于一个ajax调用(DELETE)而不适用于另一个(POST)?
答案 0 :(得分:1)
csrf令牌未在POST页面上设置如下:
<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
但是在DELETE按钮工作的页面上,他们已经设置好了。由于它们未定义,导致jQuery爆炸。