-1

Access to XMLHttpRequest at 'http://www.----------.com/efsapi/api/AdvanceQuote? segmentcode=--&customercode=--&por=--&fdc=--&toscode=--' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource (edited)

This error in i am facing in angularjs and we are using java as backend. tried the below code in front-end

'Access-Control-Allow-Origin': true,
 'Content-Type': 'application/json; charset=utf-8',
 "X-Requested-With": "XMLHttpRequest

Could you please tell how to handle this CORS error? (Platforms Angularjs & Java)

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Hemanth
  • 193
  • 1
  • 5
  • 18

1 Answers1

3

The problem you face is that the java backend blocks the client request and you need to allow the origin of the client in the backend.

From the spring cors guide

Enabling CORS for a single method:

So that the RESTful web service will include CORS access control headers in its response, you just have to add a @CrossOrigin annotation to the handler method:

@CrossOrigin(origins = "http://localhost:9000")
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(required=false, defaultValue="World") String name) {
    System.out.println("==== in greeting ====");
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
}

Enabling CORS globally:

As an alternative to fine-grained annotation-based configuration, you can also define some global CORS configuration as well. This is similar to using a Filter based solution, but can be declared within Spring MVC and combined with fine-grained @CrossOrigin configuration. By default all origins and GET, HEAD and POST methods are allowed.

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

For reference take a look at the original source: https://spring.io/guides/gs/rest-service-cors/

BeWu
  • 1,941
  • 1
  • 16
  • 22
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/24654998) – nbk Nov 22 '19 at 08:16
  • @nbk thanks for the reminder I revised the answer and included the relevant excerpts from the guide. – BeWu Nov 22 '19 at 08:40