I'm trying to get a json data from a server that doesn't support CORS. I read the Jsonp is the solution for that kind of stituation but i still can't make it work : My code :
var headers = {
'Authorization': 'Bearer ' + 'token',
'Accept': 'application/json'
};
function myCallbackFunction(data){
$('body').text(data.response);
}
$.ajax({
type: "GET",
headers : headers,
url: "https://site/api/etc",
dataType: "jsonp",
success: function(data){console.log(data);},
jsonp: false,
jsonpCallback : "myCallbackFunction",
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
i get this
error: Uncaught SyntaxError: Unexpected token :
i know that's because the server returns a json data but i don't know how to correct that because if change data type to json i get a CORS related error :
XMLHttpRequest cannot load https://site/api/1.0/etc. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://site' is therefore not allowed access.
that means CORS not supported in the server side but i use postman it works i don't understand. i've tried a lot of other thing but could get the data . Please help . Thank you. PS : i'm under a tomcat server
I'll just put what worked for me here : So what i did is create a rest controller using "okhttp" instead of an ajax call this way there won't be a context creating the problem then retreive the data and manipulate it with js .