简体   繁体   English

JSON使用JQuery获取请求(跨域)

[英]JSON Get request using JQuery (cross-domain)

I'm trying to make a simple JSON get request to an API on a domain that I do not control. 我正在尝试向我无法控制的域上的API发出简单的JSON get请求。

My code is simply: 我的代码很简单:

$(document).ready(function () {
        $.ajax({
            type: 'GET',
            url: 'http://pubapi.cryptsy.com/api.php?method=marketdatav2',
            success: function (data) {
                console.log(data);
            }
        });
});

But since that is a cross-domain request, I am getting this error in the Chrome Console: 但由于这是一个跨域请求,我在Chrome控制台中收到此错误:

XMLHttpRequest cannot load http://pubapi.cryptsy.com/api.php?method=marketdatav2. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.

And when I try to add the parameter dataType: 'jsonp' the Console returns with this error: 当我尝试添加参数dataType: 'jsonp' ,Console返回此错误:

Uncaught SyntaxError: Unexpected token : 未捕获的SyntaxError:意外的令牌:

But when I examine the Network tab in Chrome I see that under Headers the Status Code is 200 OK and I can actually see the full response in the Response tab, but the console is still showing the "Unexpected Token :" error and the JQuery JSON request is still failing. 但是当我检查Chrome中的Network选项卡时,我看到在Headers下,Status Code为200 OK,我可以在Response选项卡中看到完整的响应,但是控制台仍然显示“Unexpected Token:”错误和JQuery JSON请求仍然失败。

Here's the JS Fiddle link: http://jsfiddle.net/6Qcq2/ You can see the same results 这是JS Fiddle链接: http//jsfiddle.net/6Qcq2/您可以看到相同的结果

I have tried running the url on http://www.hurl.it and it shows me Status OK and the response as well, so I must be doing something wrong. 我试过在http://www.hurl.it上运行网址,它显示状态OK和响应,所以我一定做错了。

I've pretty much wasted the whole day trying to figure out how to get around this problem. 我整天都在试图弄清楚如何解决这个问题。

Your help is very much appreciated. 非常感激你的帮助。

The response from the API is JSON, not JSONP, so just changing the data type doesn't help. API的响应是JSON,而不是JSONP,因此仅更改数据类型无济于事。

You can use a proxy that makes the request and turns the JSON into JSONP: 您可以使用发出请求的代理并将JSON转换为JSONP:

$(document).ready(function () {
    $.ajax({
        type: 'GET',
        url: 'http://jsonp.guffa.com/Proxy.ashx?url=pubapi.cryptsy.com%2fapi.php%3fmethod=marketdatav2',
        dataType: 'jsonp',
        success: function (data) {
            console.log(data);
        }
    });
});

Demo: http://jsfiddle.net/6Qcq2/1/ 演示: http//jsfiddle.net/6Qcq2/1/

You need to setup some type of proxy script. 您需要设置某种类型的代理脚本。 Due to the Same-origin policy , you can't make an ajax call to a resource that is on an external domain. 由于Same-origin策略 ,您无法对外部域上的资源进行ajax调用。 You can get around this by setting up a simple PHP script that will query the data for you. 您可以通过设置一个简单的PHP脚本来解决这个问题,该脚本将为您查询数据。 Then, you would point your ajax call to your script (which will be hosted on your domain). 然后,您可以将ajax调用指向您的脚本(将在您的域上托管)。 The content type for that resource is application/json, so telling jQuery the type is jsonp won't help you. 该资源的内容类型是application / json,因此告诉jQuery类型是jsonp对你没有帮助。

AJAX requests do not work cross-domain for security reasons. 出于安全原因,AJAX请求不能跨域工作。 Since you're reading JSON data, you may be able to make JSONP work. 由于您正在阅读JSON数据,因此您可以使JSONP工作。

Shouldn't the jsonp response direct to a callback? jsonp响应不应该直接回调吗?

What is JSONP all about? 什么是JSONP?

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

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