When I call my api get method from Postman client it works and I get json result. I want to get the same result from ajax call. This is a my API URL without any authentication : http://localhost/Web.Api/api/landing
I try this `
$(document).ready(function () {
const Url = "http://localhost/Web.Api/api/landing";
$(".buttonP").click(function () {
$.ajax({
url: Url,
headers: { 'Access-Control-Allow-Origin': '*' },
//also I tried the same with the Access-Control-Allow-Methods: GET, POST, PUT, DELETE
type: "GET",
dataType: "json",
success: function (res) {
console.log(res)
},
error: function (e) {
console.log(e)
}
});
});
});
And get this error: Access to XMLHttpRequest at 'http://localhost/Web.Api/api/landing' from origin 'null' 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.
And I did some research and find this solution, I added [crossDomain: true, dataType: 'jsonp' ],`
$(".buttonP").click(function () {
$.ajax({
url: Url,
headers: { 'Access-Control-Allow-Origin': '*' },
type: "GET",
crossDomain: true,
dataType: 'jsonp',
success: function (res) {
console.log(res)
},
error: function (e) {
console.log(e)
}
});
});
Then error regarding CORS policy disappear, and the call is reaches to api get method, and in response I see status code 200, but error function is fires and I can't get json result. So what am i doing wrong ?
Aucun commentaire:
Enregistrer un commentaire