[英]Fetching data from Restful webservice with JavaScript?
我有幾個問題。 首先是我不知道如何使用javascript從網絡服務獲取數據。 第二個是,當我嘗試以JSON格式而不是XML來獲取數據時,我總是會得到一個xml。 我真的是JS,Jquery之類的新手,所以我才開始研究它。
http://localhost:8080/UserRest/webresources/users?format=json // Even If format is JSON the result is always XML. Browser is Chrome
<users>
<user>
<age>40</age>
<firstName>Mark</firstName>
<freeText>Free</freeText>
<id>1</id>
<lastName>Lake</lastName>
<weight>200</weight>
</user>
<user>
<age>30</age>
<firstName>Sam</firstName>
<freeText>Words</freeText>
<id>2</id>
<lastName>Grass</lastName>
<weight>105</weight>
</user>
</users>
如果我嘗試
http://localhost:8080/UserRest/webresources/users?format=XML
在Safari中,我得到了:
40馬克免費1湖20030山姆字2草105
網絡服務:
@GET
@Override
@Produces({"application/xml", "application/json"})
public List<User> findAll() {
return super.findAll();
}
頁:
function fetchUserFunction()
{
$.getJSON("http://localhost:8080/UserRest/webresources/users?format=json",
function(data) {
$.each(data, function(key, value) {
alert(key + "=" + value);
$('#maindiv').append('<li id="' + key.toString() + '">' + value.toString() + '</li>');
});
});
}
</script>
</head>
<body>
<h1>Users</h1>
<a href="http://localhost:8080/UserRest/webresources/users"> Test restful service</a>
<button onclick="fetchUserFunction()">Fetch Users</button>
<div id="maindiv"></div>
</body>
</html>
問題:
如何使用JSON或/和Jquery或僅使用純js從JavaScript提取頁面數據? 有人可以對我的js函數實現基本情況並解釋原因和原因。 我的函數只返回[object Object],[object Object]嗎?
如何使數據為JSON格式而不是XML?
從獲取的數據構建表的最佳方法是什么?
我可以在webservice類中調用特定的方法,如何調用,例如findAll()?
我正在使用JPA和JAAS身份驗證。 在我使用Web服務並且客戶端可以位於不同服務器的情況下,如何避免緩存問題? 有時數據是舊的,因為Eclipselink是從緩存而不是從DB讀取數據的?
你過得好嗎?
這適合一個人問很多問題嗎?
佐美
@GET
@Produces({"application/xml", "application/json"})
public String doGetAsXmlOrJson() {
...
}
如果可接受的媒體類型為“ application / xml”和“ application / json”,則將調用doGetAsXmlOrJson方法。 如果兩者均可接受,則將選擇前者,因為它首先出現。
在http://jersey.java.net/nonav/documentation/snapshot/jaxrs-resources.html找到了
要將JSON轉換為JavaSCript,您可以調用
res = JSON.parse(responseJson);
res將成為一個JS對象,您可以在其中輕松進行任何操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.