I want to use an API for a Wallpapers page but I ran into the ajax cross domain restriction. Please investigate the error and it can be fixed by using JSONP from JQuery to grab the information returned by the API. This is my request.
$.ajax({
url: endpoint,
type: 'GET',
dataType: 'jsonp',
data: { 'auth': apikey ,
'method':'search',
'term': keyword},
success: function(data){
console.log(data);
}
})
I also tried this way
$.getJSON(urlsearch+"?callback=?",function(result){
console.log(result);
})
It doesn't work either way. I already checked that the API works correctly, This is the error that appears in firebug.
I read that maybe the server does not support JSONP or CORS and that a solution could be to create a script in PHP that obtains the information and transfers it to javascript. But I'm not sure it's the best way to do it. Does anyone know any solution?
Thanks in advance.
CORS
activated. However, the fact that it has it does not mean that you can connect if it is not public. In this case, you would need to authenticate yourself (user/password, token, key, etc.)1
, then you make a simple request to the server to parse the response. If it doesn't supportCORS
it you will see it in the response.From what your code shows, you are authenticating yourself to the server with a key (
'auth': apikey
). And this is reflected in the console, since you are getting a response (you can see that an array of objects arrives with data such as width and height).The message you get:
It is likely because:
JSON
is not the same asJSONP
. A responseJSONP
is basically ascript
containing an execution of a (default) function. An example of a responseJSONP
:conclusion
What you have to do is change
jsonp
tojson
in order to have aJSON
valid one.