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."
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