jsonp请求不在firefox中工作

时间:2022-12-06 19:26:09

I am trying a straightforward remote json call with jquery. I am trying to use the reddit api. http://api.reddit.com. This returns a valid json object.

我正在尝试使用jquery进行简单的远程json调用。我正在尝试使用reddit api。 http://api.reddit.com。这将返回一个有效的json对象。

If I call a local file (which is what is returned from the website saved to my local disk) things work fine.

如果我调用本地文件(从保存到本地磁盘的网站返回的文件),一切正常。

$(document).ready(function() {
    $.getJSON("js/reddit.json", function (json) {
        $.each(json.data.children, function () {
            title = this.data.title;
            url = this.data.url;
            $("#redditbox").append("<div><a href=\"" + url + "\">" + title + "</a><div>");
        });
    });
});

If I then try to convert it to a remote call:

如果我然后尝试将其转换为远程调用:

$(document).ready(function() {
    $.getJSON("http://api.reddit.com", function (json) {
        $.each(json.data.children, function () {
            title = this.data.title;
            url = this.data.url;
            $("#redditbox").append("<div><a href=\"" + url + "\">" + title + "</a><div>");
        });
    });
});

it will work fine in Safari, but not Firefox. This is expect as Firefox doesnt do remote calls due to security or something. Fine.

它可以在Safari中正常工作,但不适用于Firefox。由于安全性或其他原因,Firefox不会进行远程调用。精细。

In the jquery docs they say to do it like this (jsonp):

在jquery文档中,他们说这样做(jsonp):

$(document).ready(function() {
    $.getJSON("http://api.reddit.com?jsoncallback=?", function (json) {
        $.each(json.data.children, function () {
            title = this.data.title;
            url = this.data.url;
            $("#redditbox").append("<div><a href=\"" + url + "\">" + title + "</a><div>");
        });
    });
});

however it now stops working on both safari and firefox. The request is made but what is return from the server appears to be ignored.

但它现在停止在safari和firefox上工作。发出请求但是从服务器返回的内容似乎被忽略。

Is this a problem with the code I am writing or with something the server is returning? How can I diagnose this problem?

这是我正在编写的代码或服务器返回的问题吗?我该如何诊断这个问题?

EDIT Changed the address to the real one.

编辑将地址更改为真实地址。

4 个解决方案

#1


The URL you are pointing to (www.redit.com...) is not returning JSON! Not sure where the JSON syndication from reddit comes but you might want to start with the example from the docs:

您指向的URL(www.redit.com ...)未返回JSON!不确定来自reddit的JSON联合的来源,但您可能想从文档中的示例开始:

$(document).ready(function() {
  $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function (data) {
    $.each(data.items, function(i,item){
        $("<img/>").attr("src", item.media.m).appendTo("#redditbox");
        if ( i == 4 ) return false;
      });

});

});

(apologies for formatting)

(格式化道歉)

EDIT Now I re read your post, I see you intended to go to api.reddit.com unfortunately you haven't got the right parameter name for the json callback parameter. You might need to further consult the reddit documentation to see if they support JSONP and what the name of the callback param should be.

编辑现在我重新阅读你的帖子,我发现你打算去api.reddit.com,遗憾的是你没有得到json回调参数的正确参数名称。您可能需要进一步查阅reddit文档,看看它们是否支持JSONP以及回调参数的名称应该是什么。

#2


JSONP is something that needs to be supported on the server. I can't find the documentation, but it appears that, if Reddit supports JSONP, it's not with the jsoncallback query variable.

JSONP是服务器上需要支持的东西。我找不到文档,但看起来,如果Reddit支持JSONP,那么它不是jsoncallback查询变量。

What JSONP does, is wrap the JSON text with a JavaScript Function call, this allows the JSON text to be processed by any function you've already defined in your code. This function does need to be available from the Global scope, however. It appears that the JQuery getJSON method generates a function name for you, and assigns it to the jsoncallback query string variable.

JSONP的作用是使用JavaScript函数调用包装JSON文本,这允许JSON文本由您已在代码中定义的任何函数处理。但是,此功能确实需要从全局范围中获得。似乎JQuery getJSON方法为您生成函数名称,并将其分配给jsoncallback查询字符串变量。

#3


I'm not sure about reddit.com, but for sites that don't support the JSONP idiom you can still create a proxy technique (on the backend) that would return the reddit JSON, and then you would just make an ajax request to that that.

我不确定reddit.com,但对于不支持JSONP习惯用语的网站,您仍然可以创建一个代理技术(在后端),它将返回reddit JSON,然后你只需要发一个ajax请求那个那个。

So if you called http://mydomain.com/proxy.php?url=http://api.reddit.com:

所以如果你打电话给http://mydomain.com/proxy.php?url=http://api.reddit.com:

<?php
$url = $_GET["url"];
print_r(file_get_contents($url));
?>

#4


http://api.reddit.com/ returns JSON, but doesn't appear to be JSONP-friendly. You can verify this, if you have GET, via

http://api.reddit.com/返回JSON,但似乎不是JSONP友好的。如果你有GET,你可以验证这一点

% GET http://api.reddit.com/?callback=foo

which dumps a stream of JSON without the JSONP wrapper.

它在没有JSONP包装器的情况下转储JSON流。

http://code.reddit.com/browser/r2/r2/controllers/api.py (line 84) shows the code looking for 'callback' (not 'jsoncallback'). That may be a good starting point for digging through Reddit's code to see what the trick is.

http://code.reddit.com/browser/r2/r2/controllers/api.py(第84行)显示了寻找'回调'的代码(不是'jsoncallback')。这可能是挖掘Reddit代码以查看诀窍的一个很好的起点。

#1


The URL you are pointing to (www.redit.com...) is not returning JSON! Not sure where the JSON syndication from reddit comes but you might want to start with the example from the docs:

您指向的URL(www.redit.com ...)未返回JSON!不确定来自reddit的JSON联合的来源,但您可能想从文档中的示例开始:

$(document).ready(function() {
  $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function (data) {
    $.each(data.items, function(i,item){
        $("<img/>").attr("src", item.media.m).appendTo("#redditbox");
        if ( i == 4 ) return false;
      });

});

});

(apologies for formatting)

(格式化道歉)

EDIT Now I re read your post, I see you intended to go to api.reddit.com unfortunately you haven't got the right parameter name for the json callback parameter. You might need to further consult the reddit documentation to see if they support JSONP and what the name of the callback param should be.

编辑现在我重新阅读你的帖子,我发现你打算去api.reddit.com,遗憾的是你没有得到json回调参数的正确参数名称。您可能需要进一步查阅reddit文档,看看它们是否支持JSONP以及回调参数的名称应该是什么。

#2


JSONP is something that needs to be supported on the server. I can't find the documentation, but it appears that, if Reddit supports JSONP, it's not with the jsoncallback query variable.

JSONP是服务器上需要支持的东西。我找不到文档,但看起来,如果Reddit支持JSONP,那么它不是jsoncallback查询变量。

What JSONP does, is wrap the JSON text with a JavaScript Function call, this allows the JSON text to be processed by any function you've already defined in your code. This function does need to be available from the Global scope, however. It appears that the JQuery getJSON method generates a function name for you, and assigns it to the jsoncallback query string variable.

JSONP的作用是使用JavaScript函数调用包装JSON文本,这允许JSON文本由您已在代码中定义的任何函数处理。但是,此功能确实需要从全局范围中获得。似乎JQuery getJSON方法为您生成函数名称,并将其分配给jsoncallback查询字符串变量。

#3


I'm not sure about reddit.com, but for sites that don't support the JSONP idiom you can still create a proxy technique (on the backend) that would return the reddit JSON, and then you would just make an ajax request to that that.

我不确定reddit.com,但对于不支持JSONP习惯用语的网站,您仍然可以创建一个代理技术(在后端),它将返回reddit JSON,然后你只需要发一个ajax请求那个那个。

So if you called http://mydomain.com/proxy.php?url=http://api.reddit.com:

所以如果你打电话给http://mydomain.com/proxy.php?url=http://api.reddit.com:

<?php
$url = $_GET["url"];
print_r(file_get_contents($url));
?>

#4


http://api.reddit.com/ returns JSON, but doesn't appear to be JSONP-friendly. You can verify this, if you have GET, via

http://api.reddit.com/返回JSON,但似乎不是JSONP友好的。如果你有GET,你可以验证这一点

% GET http://api.reddit.com/?callback=foo

which dumps a stream of JSON without the JSONP wrapper.

它在没有JSONP包装器的情况下转储JSON流。

http://code.reddit.com/browser/r2/r2/controllers/api.py (line 84) shows the code looking for 'callback' (not 'jsoncallback'). That may be a good starting point for digging through Reddit's code to see what the trick is.

http://code.reddit.com/browser/r2/r2/controllers/api.py(第84行)显示了寻找'回调'的代码(不是'jsoncallback')。这可能是挖掘Reddit代码以查看诀窍的一个很好的起点。