2017-02-22 64 views
0

我想做一個基本的職位,我得到一個'toLowerCase'錯誤。jQuery ajax'toLowerCase'錯誤與基本的POST方法,但適用於類似的DELETE方法

我的網址是「開發者/ 58ace2b191e01c24f61fc241 /打破」 和要求是「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

在代碼中你使用過'toLowerCase'嗎? – Aatman

+0

您是否嘗試過使用與DELETE相同的方法。我的意思是使用$ .ajax而不是$ .post –

回答

0

的CSRF令牌沒有在頁面上設置與POST像這樣:

<meta name="_csrf" th:content="${_csrf.token}"/> 
    <meta name="_csrf_header" th:content="${_csrf.headerName}"/> 

但是,在刪除按鈕的工作,他們在頁面上設置。由於它們未定義,導致jQuery炸燬。

相關問題