Enable CORS AngularJS to send HTTP POST request

后端 未结 5 989
眼角桃花
眼角桃花 2020-12-10 02:33

I want to send an HTTP POST request by submitting a form to my server, which is located at a different domain (enabled cors in the server script using node.js).

Thi

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 02:52

    For those users who are finding an answer to make CORS requests from Angular JS. Firstly, don't spend enough time in adding unnecessary headers in your JS configuration. You don't have to change anything in your frontend. Just add the following annotation in your controller or at the method level.

    @CrossOrigin
    

    If you want to enable CORS for any specific URL then use the following annotation,

    @CrossOrigin(origins = "http://localhost:9000")
    

    The above solution is given for the Controller method CORS configuration. You shall also do Global CORS configuration by adding the following in your application class.

    public WebMvcConfigurer corsConfigurer() {
            return new WebMvcConfigurer() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/**").allowedOrigins("http://localhost:9000");
                }
            };
        }
    

    Reference : https://spring.io/guides/gs/rest-service-cors/

    I hope this help. Peace! :)

提交回复
热议问题