Ajax JSON请求失败

3
我创建了一个简单的REST服务,用于提供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"}]}


实际错误是什么? - Jaime Torres
将 alert('Error') 替换为 alert(jqXHR.responseText); 并检查。 - Arun Unnikrishnan
alert(jqXHR.responseText)是空的。 - fetch101
在Chrome浏览器中按F12键可以查看错误信息,请在此处写下来。 - ridvanzoro
3
这个页面上运行的脚本也在http://localhost:8080/上吗?你尝试如何在Chrome中禁用SOP(同源策略)? - apsillers
显示剩余2条评论
1个回答

0

好的,看起来 SOP 是问题。 我尝试在 Chrome 中使用 --disable-web-security 禁用它。

现在我运行 html 文件并在相同的 web 服务器上休息,它可以工作。


发现了这篇关于Jersey跨域过滤器的博客文章,非常有用:http://simplapi.wordpress.com/2013/04/10/jersey-jax-rs-implements-a-cross-domain-filter/ - fetch101

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接