简体   繁体   English

停止从axios.post发送预检请求

[英]Stop sending preflight requests from axios.post

I have my micro-service developed using spring-boot and spring security and frontend is designed on react-hooks. 我已经使用spring-boot和spring安全性开发了微服务,并且前端是基于react-hook设计的。 Now, while I am send some data to my micro-service using axios.post method, it send CORS preflight method ie options method because axios by default send content-type as application/json and application.json leads to send options request to server before any other request. 现在,当我使用axios.post方法向我的微服务发送一些数据时,它会发送CORS预检方法(即options方法),因为axios默认情况下将content-type作为application / json发送,而application.json导致向服务器发送选项请求在任何其他要求之前。

I have tried sending my request with different headers and content types as 'application/x-www-form-urlencoded' also I have used @cross-origin(*) at my server end. 我尝试使用不同的标头和内容类型以“ application / x-www-form-urlencoded”形式发送请求,并且在服务器端使用了@ cross-origin(*)。

const config = {
                    headers: {
                    'Content-Type': 'application/json'
                    }
                }

            const response = await axios.post(ps.user_ms_url+ps.user_login,
                {
                    username:values.email,
                    password:values.password
                    // headers:{'tokenvalue':'token'}

                },
                config);

I expect my browser to send only post request to the server, for that I am ready to change my headers as well. 我希望浏览器仅将发布请求发送到服务器,为此,我也准备更改标头。 Any help will be appreciated. 任何帮助将不胜感激。 Thanks in advance! 提前致谢!

This looks to be server side CORS issue. 这似乎是服务器端CORS问题。 You have to allow domains to access resources by providing correct response headers. 您必须通过提供正确的响应标头来允许域访问资源。

You can look at adding CORS headers in spring boot. 您可以查看在春季启动中添加CORS标头。 Refer to this link 参考此链接

Hope that helps!!! 希望有帮助!!!

You can avoid CORS preflight request by proxying the request. 您可以通过代理请求来避免执行CORS预检请求。 Add this in your webpack development config 将此添加到您的webpack开发配置中

  devServer: {
    port: process.env.PORT || 3000,
    proxy: {
      '/api': {
        target: 'http:localhost:8080',
        pathRewrite: { '^/api': '' },
        changeOrigin: true,
      },
    },
  }

This means your request to /api/users will forwarded to http://localhost:8080/users . 这意味着您对/api/users请求将转发到http://localhost:8080/users

If you are using create-react-app. 如果您使用的是create-react-app。 just add "proxy": "http://localhost:8080" to your package.json. 只需在您的package.json中添加"proxy": "http://localhost:8080"即可。 Check more info here 在这里查看更多信息

I found the solution for my query. 我找到了查询的解决方案。 As I mentioned above, our browser sends preflight request (means options request) before any other request if our request is not simple (here simple means: if request contains content-type : application/json or custom headers etc) and if we are sending this request to some other domain/ URL. 如前所述,如果我们的请求不简单(此处简单的意思是:如果请求包含content-type:application / json或自定义标头等),并且我们正在发送,则我们的浏览器会在其他任何请求之前发送预检请求(即选项请求)将此请求发送到其他域/ URL。

And our axios.post method carries content-type as application/json by default, that's why, my browser was sending multiple requests (means preflight request before any other request). 而且我们的axios.post方法默认情况下将content-type携带为application / json,这就是为什么我的浏览器正在发送多个请求(意味着预检请求先于其他任何请求)。

Now, I have changed my request content-type to application/x-www-form-urlencoded by sending data as params, as shown below: 现在,通过将数据作为参数发送,我将请求的内容类型更改为application / x-www-form-urlencoded,如下所示:

     var params = new URLSearchParams();
                params.append('username', values.email);
                params.append('password', values.password);

            const response = await axios.post(ps.user_ms_url+ps.user_login,
                params);

And handling this request at backend using @ModelAttribute annotation (Spring-boot). 并使用@ModelAttribute批注(春季启动)在后端处理此请求。 So, keeping request simple can stop preflight requests. 因此,保持简单的请求可以停止预检请求。

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

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