[英]NetworkError: Failed to execute 'send' on 'XMLHttpRequest' (ajax、WebAPI)
[英]JQuery AJAX Error failed to execute send on xmlhttprequest
我正在嘗試使用JQuery AJAX方法執行bing映射REST API。 剩余URL在瀏覽器中顯示正確的結果。 但是,當我使用JQuery的AJAX方法執行相同的URL時,它似乎無法正常工作,並且出現以下異常:
“ Netowrk錯誤:無法在xmlhttprequest上執行發送”
這是我的代碼:
$.ajax({
type: 'GET',
async: false,
dataType: 'text',
url: 'http://dev.virtualearth.net/REST/v1/Locations/47.64054,-122.12934?includeEntityTypes=Address&includeNeighborhood=0&include=ciso2&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
success: function (result) {
alert("working");
},
error: function (xhr, ajaxOptions, thrownError, request, error) {
alert('xrs.status = ' + xhr.status + '\n' +
'thrown error = ' + thrownError + '\n' +
'xhr.statusText = ' + xhr.statusText + '\n' +
'request = ' + request + '\n' +
'error = ' + error);
}
});
當我在瀏覽器中打開相同的URL時,可以看到JSON格式的結果。
請幫忙!!!
如另一個響應中所述,您需要將數據類型設置為jsonp。 但是,它應該是對定位服務的GET請求,因為不支持跨域POST請求。 您可以在此處找到有關在Bing Maps服務和jQuery之類的各種JavaScript框架上使用的不錯的博客文章: https : //blogs.bing.com/maps/2015/03/05/accessing-the-bing-maps-rest-services -from-各種的JavaScript的框架/
我不得不解釋一下你的問題。 看來您正在使用MS的virtualearth.net/Bing地理位置find by point
API find by point
: https : //msdn.microsoft.com/en-us/library/ff701710.aspx
稍微閱讀他們的文檔,看來您正在嘗試發送經/緯度坐標並以回信的方式接收地址信息。 您已在URL中將includeNeighborhood
標志設置為0,這意味着您沒有該信息(不確定這是否是有意的)。
至於AJAX jQuery調用,我認為錯誤在於調用格式。 可能是POST
類型,因為您是將信息發布到Bing API,然后接收結果。 GET
請求將用於只讀API。
我將如何編寫此調用的示例(請注意type
和dataType
參數)。 如果要JSONP
收到的數據使用回調函數,則應該發送JSONP
而不是JSON(可能是場景)。
$.ajax({
type: 'POST',
dataType: "JSONP",
url: 'http://dev.virtualearth.net/REST/v1/Locations/47.64054,-122.12934?includeEntityTypes=Address&includeNeighborhood=0&include=ciso2&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
success: function (resultPass) {
console.log(resultPass);
},
error: function (resultFail) {
console.log(resultFail);
}
});
我遇到了同樣的問題,即Failed to execute 'send' on 'XMLHttpRequest'
(僅出現在Chrome中),最后發現問題出在使用async: false
選項上。 我想這個錯誤是由於試圖向外部域發出同步請求而引起的,這不支持,如jQuery文檔中所述 :
跨域請求和dataType:“ jsonp”請求不支持同步操作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.