2013-10-23 196 views
3

我创建了一个简单的休息服务,为我提供JSON。我可以通过浏览器通过http://localhost:8080/WebTodo/rest/todos访问它,并获取带有JSON的ToDos列表。 (这是本教程:http://www.vogella.com/articles/REST/article.html#crudAjax 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"}]}

+0

什么是实际错误? –

+0

用alert(jqXHR.responseText)替换alert('Error');并检查。 – Arunu

+0

它执行'错误:函数(jqXHR,状态){ 警报(“错误”) }' 我没有看到实际的错误信息 – fetch101

回答

0

OK了acutal JSON,它看起来像SOP是问题。 我试图用-disable-web-security在Chrome中禁用它。

我现在运行的HTML文件,并在同一个网络服务器上休息,它的工作原理。

+0

找到了关于跨领域过滤球衣的博客文章,非常有用:http://simplapi.wordpress.com/2013/04/10/jersey-jax-rs-implements-a-cross-域的过滤器/ – fetch101