php后端接口无法跨域

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

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

然而前端调用接口请求数据时仍然报错:

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

阅读 4.9k
3 个回答

你可以使用callback的方式去解决前端的跨域问题

你设置的 Access-Control-Allow-Origin 得看你的Response Headers有没有
有些框架会覆盖你的设置

clipboard.png

还要设置
Access-Control-Allow-Method: GET POST OPTIONS PUT

你header都没加上, *前面少个空格

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题