1
無法理解此代碼總是在控制檯"Error!"
中打印的原因。Ajax請求不適用於Spring控制器
這裏是我的春天控制器
@RequestMapping("/spinner")
public class SpinnerController {
@RequestMapping(method = RequestMethod.GET,
produces = "application/json")
public @ResponseBody String spinner() throws InterruptedException {
Thread.sleep(10);
return "answer";
}
}
而我的JS腳本:
function sendRequest() {
$.ajax({
url: '/spinner',
type: 'get',
contentType: "application/json",
success: function (resp) {
alert("Yeah!");
console.log(resp);
},
error: function(){
console.log("Error!");
}
}
);
}
和JSP頁面:
<html>
<head>
<link type="text/css" rel="stylesheet" href="../resources/css/style.css"/>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
<script type="text/javascript" charset="utf-8" src="../resources/js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="../resources/js/send.js"></script>
</head>
<body>
<button class="pure-button pure-button-primary" onclick="sendRequest()">Press me!</button>
<div id="spinner">Greeting!</div>
</body>
</html>
任何想法,爲什麼我得到一個錯誤?
UPD
這裏是日誌腳本的方法錯誤
error: function (jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
控制檯輸出:
[object Object] send.js:14
parsererror send.js:15
SyntaxError: Unexpected token a
UPD2
與添加這個固定代碼:
@RequestMapping(method = RequestMethod.GET,
produces = "application/json")
public @ResponseBody Answer spinner() throws InterruptedException {
Thread.sleep(10);
return new Answer("info");
}
public class Answer {
private String data;
public Answer(String data) {
this.data = data;
}
public Answer() {
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
控制檯「錯誤!」 - 在哪裏?在瀏覽器控制檯或服務器控 – Abhi 2014-08-27 22:18:43
傳遞給$ .ajax()的'error'函數接收3個參數:(jqXHR jqXHR,String textStatus,String errorThrown)。嘗試添加並檢查其值 – Twicetimes 2014-08-27 22:24:13
@Abhi在瀏覽器中。 – barbara 2014-08-27 22:26:14