使用 Axios 的预检响应具有无效的 HTTP 状态代码 400
Response for preflight has invalid HTTP status code 400 using Axios
我正在使用 Axios 库在我的 React 中进行 API 调用 application.I 调用 API,然后使用 React 填充 table。
我的axios调用如下:
axios({
method: 'get',
url: DataURL,
headers: {
'Content-Type' : 'application/json',
'Id': user.Id,
'Name' : user.Name,
'api-token' : user.access_token,
'clientId' : 'web',
},
responseType: 'json',
})
.then((response) => {
this.setState({ tableData: response.data });
});
但是我得到这个错误:
XMLHttpRequest cannot load MY API URL Response for preflight has invalid HTTP status code 400
同样在我的开发环境中工作,我没有添加任何 headers,但是在迁移到需要我添加 headers 的新环境之后,我收到了上述错误.
我的问题是,这是客户端问题(如错误的 header 格式等)还是与服务器端处理 API 调用有关?
我认为这是一个服务器端问题。如果您在后台使用节点,则需要 CORS 作为中间件 (https://www.npmjs.com/package/cors)。
对于其他服务器解决方案,当然还有 cors 请求处理程序。
我正在使用 Axios 库在我的 React 中进行 API 调用 application.I 调用 API,然后使用 React 填充 table。
我的axios调用如下:
axios({
method: 'get',
url: DataURL,
headers: {
'Content-Type' : 'application/json',
'Id': user.Id,
'Name' : user.Name,
'api-token' : user.access_token,
'clientId' : 'web',
},
responseType: 'json',
})
.then((response) => {
this.setState({ tableData: response.data });
});
但是我得到这个错误:
XMLHttpRequest cannot load MY API URL Response for preflight has invalid HTTP status code 400
同样在我的开发环境中工作,我没有添加任何 headers,但是在迁移到需要我添加 headers 的新环境之后,我收到了上述错误. 我的问题是,这是客户端问题(如错误的 header 格式等)还是与服务器端处理 API 调用有关?
我认为这是一个服务器端问题。如果您在后台使用节点,则需要 CORS 作为中间件 (https://www.npmjs.com/package/cors)。 对于其他服务器解决方案,当然还有 cors 请求处理程序。