2

As the title says I'm attempting to retrieve a json from an api given by the site. I've tried a variety of things now and have gotten varied results. I want to be able to retrieve and parse the json to get the information that I want out of it. Here's what I've tried:

1) $.ajax() Code chunk (runs when a button is clicked):

  $.ajax({
            type: 'GET',
            url: url,
            dataType: 'json',
            success: function(data) {
                alert('Success!');
            }
        });

This produces a "Origin null is not allowed by Access-Control-Allow-Origin." error and does not get a response from the server (for Chrome or FF, I don't care about IE since this is a small project for my use). Looking around I thought the problem might be that I need it to be a jsonp dataType since I am trying to connect to an outside website. This lead me to try #2

2) $.ajax with jsonp dataType

$.ajax({
        type: 'GET',
        url: url,
        dataType: 'jsonp',
        success: function(data) {
            alert('Success!');
        }
    });

I also appended "&callback=?" to the end of "url" that I give to the function. Using Chrom's Dev Tool I can see a response from the server this time but the alert never displays. I used JSONLINT to confirm that the response was a proper json (it is) and I've tried setting the json to a variable so I can play with it (along the lines of initializing a variable earlier in the script tag [var response;] and trying to assign the json to it[response = data;]). This produced undefined when I tried to alert(response); later on (I don't believe the response=json; bit ever got called for some reason).

I've also tried using $.getJSON but looking at the api for it it apparently just runs $.ajax anyway (I luckily got the same responses/errors when trying json vs jsonp for $.getJSON as I did when trying $.ajax). When I try as a jsonp Chrome (FF doesn't produce this error) shows a "Unexpected Syntax Error: Unexpected token :". This makes me think that the site I'm trying to talk to doesn't have jsonp working and I can't access the third party site as just a json request. The link talks about how setting the server to return as application/json rather than text/html, like I get from my response, fixed the problem for them (but again, I'm trying to access a third party site and thus I can't access the server).

I've tried this in Chrome and FF and looked at Dev Tools/Firebug for each and seen the same thing (though FF doesn't produce the origin error, but that's apparently a bug with Chrome anyway).

Also: I've taken the json response returned and run $.parseJSON on it and been able to grab various pieces but how can I access the json once I get $.ajax/$.getJSON working?

Any thoughts/solutions would be greatly appreciated.

4

5 回答 5

0

试试 JSONP 插件。它是我推荐的为数不多的插件之一,只是因为它很轻并且可以做它应该做的事情。它还允许您检查成功以外的响应。非常适合 JSONP,并解决了我在使用子域时遇到的一个问题,并且没有得到正确的错误响应(随后只是停止了代码)。

在这里得到它

于 2012-04-20T20:07:34.673 回答
0

我也曾经遇到过Unexpected Syntax Error: Unexpected token :错误。

该网站似乎不支持跨域加载。您要使用什么 API?

于 2012-04-20T20:01:56.157 回答
0

响应很可能是有效的 JSON,但不是有效的 JSONP。第三方服务器必须支持 JSONP 才能获得 JSONP 响应。如果您无法控制第 3 方服务器,则唯一真正的选择是使用服务器端代理或 YQL。

于 2012-04-20T20:02:09.147 回答
0

不要将 JQuery AJAX 用于 JSONP。

使用<script type='text/javascript' src=' URL_GOES_HERE&callback=BLAH '></script>which 将使用 javascript 方法调用围绕 json 对象,然后您可以使用来自 3rd 方源的数据。

http://en.wikipedia.org/wiki/JSONP

于 2012-04-20T20:02:37.493 回答
0

你试过这种方式吗?

$.getJSON( url + "?callback=?", function(data) {
    console.info(JSON.stringify(data, null, 2));
});

这基本上就是我如何管理我的 JSONP 回调到http://freegeoip.net/json/

于 2016-12-30T04:02:45.400 回答