2013-10-23 251 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