關於Access Control Allow Origin。查了網路,SO裡頭有好多人有同樣的問題,發現最好的解決方式就是透過server來設定response的header。
我使用Chrome來debug,發現正要用的domain會有下列回應,套上先人使用的domain來測試,沒有這樣的回應,所以猜想是server設定的問題囉~
遇到串API的問題,因為我是用AngularJS本機端開發,要串接http://happystudio.com.tw/api,debug時會遇到:
XMLHttpRequest cannot load http://happystudio.com.tw/api/Login.php?validatecode=ac2f4acce75e503a68e830…ba4&Account=a… No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘http://localhost:8100’ is therefore not allowed access.
查詢原因後,發現是安全性考量(A domain只能給A domain存取,然而我們希望B domain也能存取 A domain),於是要透過server來設定,就能順利取得回傳資料。
研究後我發現,後台應該只要設定:
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *
於是合作夥伴小彥幫我設定後,就再也沒有出現該問題啦~
隨意留個言吧:)~