我创建了一个简单的休息服务,为我提供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