2017-03-22 48 views
0

當我試圖做一個HTTP請求到服務器,我收到在Internet Explorer下面的網絡錯誤,它會錯誤回調函數:獲取網絡錯誤

SCRIPT7002: XMLHttpRequest: Network Error 0x2ef3, Could not complete the operation due to error 00002ef3

和在Chrome和Firefox它會錯誤回調函數,並顯示以下錯誤:

XMLHttpRequest cannot load the url. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

下面是代碼:

HTTPRequestConts.GET_NAV = https://abc.xyz.com:8090/autoregisterbymobileno?id=ODgyODAyMDMzNg;;&cver=ios_3.0.1&dev=QTMzNkZCQUEtREVEQy00RUE4LTlDQTYtMjg0RDk0QkIyRDRB&lv=0&imei=MzUzMjg1MDcyNjY2ODQxMA;;&oem=100001&dm=QXBwbGU;&osv=aU9TMTAuMC4y"; 
var c = new HTTPRequest(HTTPRequestConts.GET_NAV); 
c.setRequestMethod(HTTPRequestConts.GET), c.setCINResponseParser(), 
c.setCallback(new NavProxyCallback({ 
    onSuccess: function onSuccess(response) { console.log('success' + JSON.stringify(response)); }, 
    onError: function onError(err) { 
     console.log('failure' + JSON.stringify(err)); }}, false)), 
    HTTPClient.getInstance().send(c); 
+0

http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource – soorapadman

+0

請將你正在嘗試執行的代碼添加到問題,以便您可以輕鬆幫助。 –

回答

0

您正在提出跨產地請求。

當您從另一個域或端口請求資源而不是第一個資源本身提供的資源時,會發生這種情況。 https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

根據造成它的原因,您可以讓服務器處理請求並將其發送到客戶端,也可以在請求服務器上啓用交叉源域(未完成)啓用所有域)

你如何做到這一點取決於服務器。例如在節點快遞 https://enable-cors.org/server_expressjs.html 請注意,在該鏈接,它啓用所有。

res.header("Access-Control-Allow-Origin", "*"); 

取而代之的*與您自己的域名。

-1

將contentType設置爲'application/json; charset = utf-8'來解決問題。

+0

您也可以參考此鏈接 - > http://stackoverflow.com/questions/14527387/script7002-xmlhttprequest-network-error-0x2ef3-could-not-complete-the-operati –

+0

爲什麼要解決這個問題?將它添加到請求沒有任何意義 - 這是一個GET請求 - 並且錯誤消息是權限問題,JSON在那裏沒有得到特別處理。 – Quentin