HTML頁面通過Ajax調用公網web服務時,瀏覽器請求發送成功,但是響應的 xhr.status==0,控制台報錯如下
XMLHttpRequest cannot load http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
服務端為公網,是允許跨域訪問的,所以原因出在客戶端,即瀏覽器方面——谷歌瀏覽器默認不支持跨域訪問。
解決方法:
關閉瀏覽器窗口-》右擊谷歌瀏覽器圖標-》屬性。彈出屬性對話框。在目標欄最后添加 --args --disable-web-security --user-data-dir ,使瀏覽器允許跨域訪問
打開瀏覽器,顯示如下信息說明瀏覽器已更改為允許跨域訪問
當然,不是經常進行基於瀏覽器的跨域訪問的話建議還是不要開啟跨域。