我創建了一個簡單的休息服務,爲我提供JSON。我可以通過瀏覽器通過http://localhost:8080/WebTodo/rest/todos
訪問它,並獲取帶有JSON的ToDos列表。 (這是本教程:http://www.vogella.com/articles/REST/article.html#crud)Ajax JSON請求失敗
現在我想用這個簡單的HTML獲得通過jQuery的JSONs:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script>
<script>
function getJsonFunc() {
console.log("find all");
$.ajax({
type: "GET",
url: "http://localhost:8080/WebTodo/rest/todos",
contentType: "application/json",
dataType: "json",
success: function (data, status, jqXHR) {
alert('Success')
},
error: function (jqXHR, status) {
alert('Error')
}
});
}
function writeJson(todo) {
console.log("after json");
var items = [];
$.each(todo, function(key, val) {
items.push("<li id='" + key + "'>" + val + "</li>");
});
$("<ul/>", {
"class": "my-new-list",
html: items.join("")
}).appendTo("body");
}
</script>
</head>
<body>
<button id="search" onClick="getJsonFunc()">get</button>
</body>
</html>
這總是返回我的錯誤警報。我已經嘗試過使用不同的瀏覽器,並且在Chrome中禁用了相同的來源策略來嘗試它。沒什麼幫助。我在這裏錯過了什麼?
這是我在瀏覽器中收到{"todo":[{"description":"Description","done":"false","titel":"Do something","uri":"2"},{"description":"Description","done":"false","titel":"Learn REST","uri":"1"}]}
什麼是實際錯誤? –
用alert(jqXHR.responseText)替換alert('Error');並檢查。 – Arunu
它執行'錯誤:函數(jqXHR,狀態){ 警報(「錯誤」) }' 我沒有看到實際的錯誤信息 – fetch101