React Post请求跨域问题

  1. 前后端分离,前端ReactJS,后端PHP(Laravel), 本地调试的时候想调用远端接口。
  2. 前端配置:在pakcage.json中配置代理如下:
"proxy": "https://www.shufadict.com/api",
  1. 后端配置:Laravel添加中间件,配置允许跨域,如下:
header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Credentials: false");
        header("Access-Control-Allow-Methods: PUT, POST, GET, DELETE, OPTIONS");
        header("Access-Control-Allow-Headers: Content-Type, Access-Token, nonce, timestamp, packagename, sign, uuid");
        header("Access-Control-Expose-Headers: *");

结果,所有get请求都ok,但是post请求都返回如下错误:

Access to XMLHttpRequest at 'https://www.shufadict.com/' (redirected from 'http://localhost:3000/common/feedback') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

求指点,困扰好几天了。

阅读 5.2k
3 个回答

header带上这个 withCredentials: true 试一下

如果你请求用的axios:

axios.defaults.withCredentials = true

自己回答一下,其实出现下面这个提示表示已经没有跨域问题了,只是后端没有返回http ok状态而已:
Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

我的post参数传的有问题,在后端拦截器中就报错了,没有返回http ok的状态。故而出现了这个错误。

推荐问题
宣传栏