08.15 php實戰——ajax跨域請求問題解決

實戰開發中我們可能會遇到下面的問題。下面逐步展開

JQ的ajax請求代碼

php實戰——ajax跨域請求問題解決

我們請求一個遠程數據接口來獲取和分配數據,訪問後有如下錯誤提示:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

php實戰——ajax跨域請求問題解決

解決辦法:在數據返回的php接口添加好head頭

header("Access-Control-Allow-Origin: *");

php實戰——ajax跨域請求問題解決

ok,搞定了,再次訪問

php實戰——ajax跨域請求問題解決

錯誤消失,數據返回正常了,so easy。


分享到:


相關文章: