简体   繁体   English

无法获取json数据,出现Cors或回调错误

[英]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'. 我正在尝试从REST API中获取一些JSON数据,但是当我使用带有dataType参数作为jsonp的AJAX请求对其进行调用时,它给了我一个jQuery'未被调用'的错误。 This is the error message: 这是错误消息:

Error: Status: parsererror Message: Error: jQuery2130732580496231094_1429605569499 was not called 错误:状态:parsererror消息:错误:未调用jQuery2130732580496231094_1429605569499

So, if I try to make this request without jsonp , using JSON as the dataType , it gives me another error about the CORS: 因此,如果我尝试使用JSON作为dataType而不使用jsonp发出此请求,则会给我有关CORS的另一个错误:

No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 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. 我知道这是一个白痴问题,但是我已经阅读了有关CORS的信息,只是不知道将它的参数放在哪里才能启用它。 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. 如果有一天我可以理解将CORS放在哪里,那我应该用另一种方式提出请求。

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: 这是请求返回的json对象,但仅出现在控制台网络部分中:

 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. 这是一台正在测试我的请求的新服务器,但这不是我的服务器域,这低于流行的rest服务器,我相信它支持json调用。 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. 您可以看到服务器使用正确的数据回答了我的请求,但是我无法将数据发送到我的JavaScript中进行打印或处理。
Thank you for any help. 感谢您的任何帮助。

Error: Status: parsererror Message: Error: jQuery2130732580496231094_1429605569499 was not called 错误:状态:parsererror消息:错误:未调用jQuery2130732580496231094_1429605569499

The server you are requesting data from has to support JSONP in order for you to make a successful JSONP request to it. 您要从中请求数据的服务器必须支持JSONP,以便您对其进行成功的JSONP请求。

See the Wikipedia article on the subject for details of how to do that. 有关如何执行此操作的详细信息,请参阅Wikipedia文章

I've read about CORS and just don't get where to put it's params to enable it. 我已经读过有关CORS的文章,只是不知道如何启用它。

In the HTTP response headers of the service you are making the request to. 在您要发出请求的服务的HTTP响应标头中。

eg Your page on example.com wants to use JS to get data from example.net . 例如,您在example.com页面希望使用JS从example.net获取数据。 Therefore example.net has to tell the user's browser that example.com is allowed access to the data. 因此, example.net必须告知用户浏览器允许example.com访问数据。

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. 要启用CORS请求,您必须允许来自服务器部分的这种类型的请求,并从客户端创建一个允许获取cors资源的请求:仅使用GET,HEAD或POST以及内容类型application / x-www-form -urlencoded,multipart / form-data或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 不是从最简单的方法,而是...解决了:D

I wrote an app with express and request on NodeJs, and using the request it returned me all the json object. 我用NodeJs上的express和request编写了一个应用程序,并使用该请求返回了我所有的json对象。 Now i could just send it to my javascript var with the express. 现在,我可以使用express将其发送到我的javascript var中。

Thanks for all the replies guys! 谢谢大家的回复! Peace. 和平。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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