I am working on my first API project and, also, I am new to this technology.
I have been successful in implementing "GET" method on Nasa's API https://api.nasa.gov/planetary/apod and also on dog API https://dog.ceo/api/breeds/image/random on both local as well as remote server.
But when I try to "GET" in the API developed by a private institution it works fine in Postman but not on my local server. It keeps showing Access to XMLHttpRequest at '(api url)' from origin '(localserver)' has been blocked by CORS policy. No 'Access-Control-Allow-Origin' header is present on the requested resource.
HTML Doc:
<!DOCTYPE html>
<html>
<head>
<title>API</title>
</head>
<body>
<div>
PRINT
</div>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script src="./script.js"></script>
</body>
</html>
AJAX Request:
$.ajax({
url: "api",
method: "GET",
data: {
"Key": "(myapikey)",
"UserID": "userid",
"Password": "pass",
"AwbNo": "someNumber"
},
success: function(data){
console.log(data);
},
}).fail(function(){
console.log("error in script");
});
I tried the solutions like including the allow-access-control-origin header but it still showed the same error. Please help.
Edit 1:
I added the the following two lines to my ajax code:
crossDomain: true,
dataType: 'jsonp'
and now it shows the following error:
Cross-Origin Read Blocking (CORB) blocked cross-origin response "(api request)" with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.
Edit 2:
Added in AJAX code:
header:{
"Access-Control-Allow-Origin":"*"
}