2016-03-26 27 views
1

我試圖從Web APIAJAX網絡API調用

http://www.streetdirectory.com/api/?mode=search&act=all&profile=sd_default&q=640724&limit=1&show_additional=0&output=json 

使用Ajax,我遇到的問題,由此我得到這個錯誤得到json數據。

var addresses = new Array(); 
getAddress(); 
var myURL = "http://www.streetdirectory.com/api/?mode=search&act=all&profile=sd_default&q=640724&limit=1&show_additional=0&output=json"; 
function getAddress() { //get addresses from streetdirectory web API 
     $.ajax({ 
      type: "GET", 
      url: myURL, 
      dataType: "json", 
      success: function(response){ 
       addresses = response; 
       alert(response); 
      }, 
      error: function(e) { 
       alert('Error: '+e); 
       console.log('Error: ' +e); 
      } 
     }); 
} 

更新時出現控制檯錯誤。 enter image description here

這似乎是因爲我直接訪問html。我已經把它移到我的本地主機上。但它仍然有錯誤,沒有任何現在...描述

enter image description here

enter image description here

我的函數調用轉移到我的函數定義後,我現在收到此錯誤後。

回答

2

您應該使用console.log而不是alert。記錄到控制檯將允許您調查返回的對象。

由於這是一個Ajax調用和錯誤處理程序被調用,很可能響應的狀態不是200

1

"If you are working on web project and want to get data from different site, Sometime you get such type of error: Cross-Origin Request Blocked"*

我認爲你可以找到一些額外的信息閱讀這篇文章cross-origin-request-blocked