實戰開發中我們可能會遇到下面的問題。下面逐步展開
JQ的ajax請求代碼
我們請求一個遠程數據接口來獲取和分配數據,訪問後有如下錯誤提示:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
解決辦法:在數據返回的php接口添加好head頭
header("Access-Control-Allow-Origin: *");
ok,搞定了,再次訪問
錯誤消失,數據返回正常了,so easy。
閱讀更多 龍城勇哥 的文章