我创建了一个简单的REST服务,用于提供JSON。我可以通过浏览器访问
现在我想使用这个简单的HTML通过JQuery获取JSON:
http://localhost:8080/WebTodo/rest/todos
获取包含ToDo列表的JSON。(这是来自http://www.vogella.com/articles/REST/article.html#crud的教程)现在我想使用这个简单的HTML通过JQuery获取JSON:
<!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中禁用了同源策略进行尝试。但都没有帮助。我错过了什么?
这是我在浏览器中接收到的实际json {"todo":[{"description":"描述","done":"false","titel":"做某事","uri":"2"},{"description":"描述","done":"false","titel":"学习REST","uri":"1"}]}
http://localhost:8080/
上吗?你尝试如何在Chrome中禁用SOP(同源策略)? - apsillers