简体   繁体   English

OPTIONS请求禁止axios 403

[英]axios 403 forbidden on OPTIONS request

I am working on an application using React+Redux and using axios to make api calls. 我正在使用React + Redux并使用axios进行API调用。

Here's a sample call which fails: 这是一个失败的示例调用:

axios.post(`${API_ROOT}${FIND_USER}${currentUserID}`, {
        headers: {
                'Authorization': token
        },
      })
    .then((response) => {
        console.log("Sucess")
    })

Request url when I see in network is something like: 当我在network看到时请求网址是这样的:

http://domainName:8080/users/findUser/1234

API call fails at OPTIONS itself and error I am receiving from backend is OPTIONS自身的API调用失败,我从后端收到的错误是

Response for preflight has invalid HTTP status code 403

It never reaches POST . 它永远不会到达POST

token is retrieved from localstorage and is something like Bearer eyJhbGci... . localstorage检索token ,就像Bearer eyJhbGci...

Backend developers are using Java and Spring . 后端开发人员正在使用Java和Spring。

Is there something wrong in the way I am calling the APIs or this problem should be resolved in the Backend? 我调用API的方式有什么问题,或者这个问题应该在后端解决吗?

This API works totally fine when tested through Postman. 通过Postman测试时,此API工作正常。

Turns out there is already a solution for this problem in the backend : 事实证明,后端已有解决此问题的方法:

How to configure CORS in a Spring Boot + Spring Security application? 如何在Spring Boot + Spring Security应用程序中配置CORS?

It works like a charm ! 它就像一个魅力!

'headers' always come as 3rd parameter in POST request of axios. 'headers'总是作为axios的POST请求中的第3个参数。 Give 2nd arguement if there is any or pass 2nd parameter as empty String. 如果存在任何或将第二个参数传递为空字符串,则给出第二个争论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM