简体   繁体   中英

React on NodeJS can POST to Spring Boot REST API but can't PUT to the same API - CORS issue

My React app running on Node can make GET and POST calls to my Spring Boot REST API but when it tries to call PUT the browser console shows the OPTIONS request returning 403 and the following message in the console:

Failed to load http://localhost:8080/project/65 : Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:3000 ' is therefore not allowed access. The response had HTTP status code 403.

I can make the same PUT call from Postman and that works fine.

My Spring Boot app has the following CORS configuration but this is only applies to HTML served from itself and has no impact on the React app?

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("http://localhost:8080", "http://localhost:3000");
    }
}

A lot of Googling of enabling CORS in Node has turned up lots of stuff about Express but this doesn't seem to help.

As the browser works as enforcing cors, it is difficult to allow cors for localhost (from the browser).

Btw: that's why it works with tools (except running in a browser) , as they are no browsers, no enforcing happens.

The easiest way would be to run a Middleware node express server.

As a http server does not fill the request with cors, there should be no cors problem anymore.

So the process would be the following:

Frontend call request to Local http Server , Local http Server in turn makes a request to the PayPal server So:

Frontend->localhttp - >PayPal server->localhttp->frontend

Frontend and http are not same origin, so Frontend request to local http does not care about cors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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