-1
我在我的應用程序中使用jQuery和REST,我想在我的網頁中使用jQuery獲得下面提到的輸出。 我使用下面的代碼通過編號獲取公司(每個公司都有id,名稱爲其他信息供應商和買家)來搜索以下代碼,但結果沒有顯示出我的代碼,對於我錯過了什麼的任何建議?無法使用jQuery獲取輸出結果
REST是HTTP請求交換的概念,因此您正在針對您在服務器端實現的REST-API進行RESTful請求調用(例如'get')。
<input name="find" type="text" maxlength="300" id="find"/>
<button onclick="findId()"> Find By ID </button>
<div id="info"></div>
<script>
function findId()
{
var id = document.getElementById("find").value;
$("#info").html("");
$.getJSON("http://localhost:8080/company/" + id, function(data)
{
for (var i in data) {
$('#info').append("<p>ID: " + data[i].id + "</p>")
$('#info').append("<p>Name: " + data[i].name + "</p>")
$('#info').append("<p>Other Info: " + data[i].otherInfo + "</p><br>")
$('#info').append("<p>Supplier: " + data[i].suppliers + "</p><br>")
$('#info').append("<p>Buyers: " + data[i].buyers + "</p><br>")
}
});
}
當我鍵入http://localhost:8080/company/
到我的瀏覽器我得到以下輸出:
[{"id":1,"name":"Test 1","otherInfo":"Test 1","suppliers":[{"id":1,"name":"Test 1","address":"Test 1","buyers":[{"id":1,"name":"Test 1","address":"Test 1"}]},{"id":2,"name":"Test 2","address":"Test 2","buyers":[{"id":3,"name":"Test 3","address":"Test 3"},{"id":2,"name":"Test 2","address":"Test 2"}]}]},{"id":2,"name":"Test 2","address":"Test 2","suppliers":[{"id":3,"name":"Test 3","address":"Test 3","buyers":[{"id":4,"name":"Test 4","address":"Test 4"}]}]}]
如果鍵入http://localhost:8080/company/1
到我的瀏覽器,我得到
{"id":1,"name":"Test 1","otherInfo":"Test 1","suppliers":[{"id":1,"name":"Test 1","address":"Test 1","buyers":[{"id":1,"name":"Test 1","address":"Test 1"}]},{"id":2,"name":"Test 2","address":"Test 2","buyers":[{"id":3,"name":"Test 3","address":"Test 3"},{"id":2,"name":"Test 2","address":"Test 2"}]}]}
爲什麼'java'標籤? – shmosel
getJSON調用中'id'的值是什麼?如果您在瀏覽器的網址末尾輸入該ID,會發生什麼情況?所以,如果id = 7,如果在瀏覽器中輸入http:// localhost:8080/company/7會發生什麼? – DaveH
那麼你確定被追加到url的值是1還是2? – DaveH