Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi When i call rest api from java script , i will get an error
"Access to XMLHttpRequest at 'https://localhost:44300/api/sample?Empno=1234' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status."


What I have tried:

Java
<pre>Below the java script i used

 const formData = new FormData();
 const response = new XMLHttpRequest();
 response.open("GET", "https://localhost:44300/api/sample?Empno=1234");

response.setRequestHeader('key', '@1234');
response.send(formData);

response.onload = (e) => {
    alert(response.response);
}

And below the jquery

  alert('ajax');
     $.ajax({ 
         type: "GET",
         dataType: "json",
         beforeSend: function (request) {
             request.setRequestHeader('key', '@1234');
         },
         url: "https://localhost:44300/api/sample?Empno=1234",
         success: function(data){        
            alert(data);
         }
     });

In rest api web config i added this tag for enable cros policy

   <httpProtocol>
 <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" /> 
   </customHeaders>
 </httpProtocol>


But when i call rest api from server side code and if using Postman Agent,it works fine, only problem when i call from java script or jQuery,Pls help to resolve this problem.

Note:Rest api and call rest api on same machine

Regards and Thanks Aravind
Posted
Updated 4-Mar-21 23:51pm

A CORS policy[^] is a mechanism designed to limit where requests can be made from into a particular resource. In this case, this is your REST API. The REST API is receiving the request and validating where it's coming from (in this case "localhost"), not finding it on an approved list of origins, and then blocking the request.

Depending on which framework/language you're using for your REST API, you may need to register "localhost" as an allowed origin.
 
Share this answer
 
Comments
Aravindba 4-Mar-21 23:46pm    
hi i am used C# web api 3.1, and how to register if i using in localhost, now i deployed web api in one server and i call that api from another machine using java script, now i am getting this error "Access to XMLHttpRequest at 'http://sample.domain.net/api/emp?empno=102' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status."
Chris Copeland 5-Mar-21 4:21am    
I found this online documentation[^] which seems to document how to configure CORS in Web API. As I said in my post, you need to "allow" the "localhost" domain to connect to the API.
Aravindba 7-Mar-21 23:37pm    
Thanks Chris.. its working
Quote:
Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
This is nothing to do with your CORS headers. The error is telling you that a HEAD request to your URL did not succeed.

Check your browser's developer tools, and inspect the network requests to see what the server is returning instead of an "OK" response.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900