1

I am trying to do get request from angular 5 as below:

public showHeatMap(){
        console.log("in showHeatMap")
        let headers = new Headers();
        headers.append('Access-Control-Allow-Origin' , '*');
/*      
        headers: new HttpHeaders({
            'Content-Type':  'image/png',
            'Access-Control-Allow-Origin': '*'
        }) */
        this.http.get(this.showHeatMapURL,{headers: headers}).toPromise().then((res)=>{
            console.log("reading response....")
            console.log(res);
      });
    }

but it gives me error as "Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."

5

3 Answers 3

1

You need to enable CORS in your Web Api. The easier and preferred way to enable CORS globally is to add the following into web.config

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
  </httpProtocol>
</system.webServer>
Sign up to request clarification or add additional context in comments.

1 Comment

Question is about Angular. Where is web.config in Angular?
0

If you use Spring as your backend server. You can use @CrossOrigin annotation to solve it.

Just like:

@CrossOrigin
@RestController
public class DemoController {
    @GetMapping("/getHeatMap") 
    public Object getHeatMap() {
    ...
    }
}

This annotaion is also useful to single function.

1 Comment

thanks for replay, but I am using django in backend.
-1

If you are using Spring Boot as your backend then the solution which is provided to add

@CrossOrigin just above your Controller class is working in my case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.