簡體   English   中英

使用jQuery使用REST Web服務

[英]Consume REST web service with jQuery

我有一個Web服務,在某個URL上以JSON格式顯示數據,我的問題是如何使用jQuery將這些數據存儲在變量中。 到目前為止我試過這個:

 $.ajax( { type:'Get', url:'http://localhost:8080/demo/x', success:function(data) { alert(data); } }); 

但是當我到達我放置此腳本的頁面時,它不會發出任何警報。

在此先感謝您的幫助。

您可以使用簡寫.get()和get請求。 添加失敗處理程序:

 $.get("http://localhost:8080/demo/x", function(data) { alert("Retrieved data:" + data); }).fail(function() { alert("An error has occurred"); }); 

嘗試這個:

$.ajax({
   type: 'GET', 
   url: 'http://localhost:8080/demo/x',
   data: {
      format: 'json'
   },
   dataType: 'jsonp',
   success: function(data) {
      alert('Data: ' + data);   
   },
   error: function() {
      alert('Error');
   }   
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM