简体   繁体   中英

can't get json data, giving Cors or callback error

I'm trying to get some JSON data from a REST API, but when I call it cia an AJAX request with dataType param as jsonp it gives me an error that the jQuery 'callback was not called'. This is the error message:

Error: Status: parsererror Message: Error: jQuery2130732580496231094_1429605569499 was not called

So, if I try to make this request without jsonp , using JSON as the dataType , it gives me another error about the CORS:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

I know this is an idiot question, but I've read about CORS and just don't get where to put it's params to enable it. For me, the right is to use the first method that I've used, but after looking for an answer for that problem, I just don't understand anything. If someday I could understand where to put the CORS I should be use to make the request as another way.

So they are my questions, here is my code:

$.ajax({ 
    url: 'eztvapi.re/shows/1';, 
    type: 'GET', 
    dataType: 'jsonp', 
    crossDomain: true, 
    success: function(data) { 
        //data = JSON.parse(data); 
        console.log(data); 
    }, 
    error: function(jqXHR, textStatus, errorThrown) { 
        console.log("Error: Status: " + textStatus + " Message: " + errorThrown); 
    } 
}); 

This are my headers request from the console:

    Remote Address:216.58.222.12:443
Request URL:http://eztvapi.re/shows/1?callback=jQuery213010618196451105177_1429606458524&_=1429606458525
Request Method:GET
Status Code:200 OK
Response Headers
cache-control:private
content-encoding:gzip
content-type:application/json; charset=utf-8
date:Tue, 21 Apr 2015 08:54:22 GMT
etag:W/"0hnqCn40L98JLmwY2UfUlg=="
server:nginx
status:200
vary:Accept-Encoding
via:1.1 Chrome-Compression-Proxy
x-powered-by:Express
x-response-time:32ms
x-served-by:us-la3
Request Headers
:authority:eztvapi.re
:method:GET
:path:/shows/1?callback=jQuery213010618196451105177_1429606458524&_=1429606458525
:scheme:http
accept:*/*
accept-encoding:gzip, deflate, sdch
accept-language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
chrome-proxy:ps=1429584828-1395746459-275388119-220935266, sid=a259cd2eeb457c7a40ff6367868ef723, c=win, b=2311, p=90
user-agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36
Query String Parameters
view source
view URL encoded
callback:jQuery213010618196451105177_1429606458524
_:1429606458525

And this are the json object that the request returns, BUT IT ONLY APPEARS ON THE CONSOLE NETWORK PART:

 0: {_id: "tt0944947",…}
1: {_id: "tt0903747",…}
2: {_id: "tt0898266",…}
3: {_id: "tt1520211",…}
4: {_id: "tt0773262",…}
5: {_id: "tt0460649",…}
6: {_id: "tt1475582",…}
7: {_id: "tt2193021",…}
8: {_id: "tt1796960",…}
9: {_id: "tt1119644",…}
10: {_id: "tt0411008",…}   

I've done recently a lot of requests for another rests,so i think the client's side is correctly. This is a new server that i'm testing my requests, but this is not my server domain, this belows to a popular rest server and i believe that supports json call. What is funny and i've asking myself since i've start with this new one is the response that i'm getting. You can see that server answers my request with the correctly data, but i can't get the data to my javascript, to print it or process it.
Thank you for any help.

Error: Status: parsererror Message: Error: jQuery2130732580496231094_1429605569499 was not called

The server you are requesting data from has to support JSONP in order for you to make a successful JSONP request to it.

See the Wikipedia article on the subject for details of how to do that.

I've read about CORS and just don't get where to put it's params to enable it.

In the HTTP response headers of the service you are making the request to.

eg Your page on example.com wants to use JS to get data from example.net . Therefore example.net has to tell the user's browser that example.com is allowed access to the data.

To enable CORS requests, you have to allow this type of request from your server part and create a request from your client which will be allow to get cors resources : Only use GET, HEAD or POST and content type application/x-www-form-urlencoded, multipart/form-data, or text/plain.

Your client part seems to be correct, I think you have to deal with the server to allow this type of request. you can have more informations here

Finally, after a couple of days studying this problem i've solved this. Not from the most easy way, but... solved :D

I wrote an app with express and request on NodeJs, and using the request it returned me all the json object. Now i could just send it to my javascript var with the express.

Thanks for all the replies guys! Peace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM