鍍金池/ 問答/PHP  網絡安全  HTML/ php后端接口無法跨域

php后端接口無法跨域

已經在php接口文件中添加:

header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');

然而前端調用接口請求數(shù)據(jù)時仍然報錯:

Failed to load http://aaa.com/version?test=1: Redirect from 'http://aaa.com/version?test=1' to 'http://bbb.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://ccc.com' is therefore not allowed access.

clipboard.png

clipboard.png

回答
編輯回答
陪妳哭

你header都沒加上, *前面少個空格

2018年7月29日 19:10
編輯回答
涼薄

你可以使用callback的方式去解決前端的跨域問題

2018年5月27日 19:03
編輯回答
疚幼

你設置的 Access-Control-Allow-Origin 得看你的Response Headers有沒有
有些框架會覆蓋你的設置

clipboard.png

還要設置
Access-Control-Allow-Method: GET POST OPTIONS PUT

2017年4月2日 13:54