在進行前后分離的 webapp 開發,或者 H5 移動 App 開發時,我們會使用 PC 端瀏覽器進行開發調試。但默認情況下 Ajax 請求無法跨域訪問,請求時會報如下錯誤:
XMLHttpRequest cannot load http://127.0.0.1:8080/ptmo/services/ptmo/v1/execOneSql. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8020' is therefore not allowed access.
下面介紹如何通過設置 Chrome 使其支持跨域。 由於新版本的設置方法和以前不一樣,下面分別進行介紹。
一、老版瀏覽器的跨域設置(版本號49之前)
(1)右鍵點擊 Chrome 快捷方式圖標,選擇“屬性”。
(2)在屬性頁面中的目標輸入框尾部加上: --disable-web-security
(3)點擊“應用”並關閉屬性頁面。重新打開 chrome 瀏覽器。如果瀏覽器出現提示“你使用的是不受支持的命令標記 --disable-web-security”,那么說明配置成功。
二、新版瀏覽器的跨域設置(版本號49起)
(1)在電腦上新建一個目錄,例如:C:\MyChromeDevUserData
(2)在屬性頁面中的目標輸入框里加上: --disable-web-security --user-data-dir=C:\MyChromeDevUserData
(3)點擊應用和確定后關閉屬性頁面,並打開 chrome 瀏覽器。發現有“--disable-web-security”相關的提示,說明 chrome 已經可以正常跨域工作了。
原文出自:www.hangge.com 轉載請保留原文鏈接:http://www.hangge.com/blog/cache/detail_1703.html