使用jsonp访问* API会产生意外结果

时间:2022-09-06 18:50:46

I am trying to access the * API with jsonp as datatype by doing this:

我试图通过执行以下操作以jsonp作为数据类型访问* API:

$(document).ready(function(){
        $.ajax({
          url: 'http://api.*.com/1.1/tags/php/top-answerers/month',
          dataType: 'jsonp',
        });
      });

And once I reload, I get the following in the console:

一旦我重新加载,我在控制台中得到以下内容:

"Uncaught SyntaxError: Unexpected token :"

What am I doing wrong here?

我在这做错了什么?

2 个解决方案

#1


0  

Right now the application is returning Content-Type: application/json.

现在,应用程序返回Content-Type:application / json。

You can fix this by overriding the callback function name to jsonp which will tell the server to return Content-Type: application/javascript instead:

您可以通过将回调函数名称覆盖到jsonp来解决此问题,这将告诉服务器返回Content-Type:application / javascript:

$(document).ready(function () {
    $.ajax({
        url: 'http://api.*.com/1.1/tags/php/top-answerers/month',
        dataType: 'jsonp',
        jsonp: 'jsonp',
        success: function (data) {
            alert(data.top_users.length + ' users retrieved.');
        }
    });
});

Info on jsonp ajax setting:

有关jsonp ajax设置的信息:

jsonpString

Override the callback function name in a jsonp request. This value will be used instead of 'callback' in the 'callback=?' part of the query string in the url. So {jsonp:'onJSONPLoad'} would result in 'onJSONPLoad=?' passed to the server. As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }

覆盖jsonp请求中的回调函数名称。在'callback =?'中将使用此值代替'callback' url中查询字符串的一部分。所以{jsonp:'onJSONPLoad'}会导致'onJSONPLoad =?'传递给服务器。从jQuery 1.5开始,将jsonp选项设置为false可防止jQuery将“?callback”字符串添加到URL或尝试使用“=?”转型。在这种情况下,您还应该显式设置jsonpCallback设置。例如,{jsonp:false,jsonpCallback:“callbackName”}

#2


0  

From https://api.stackexchange.com/docs

All API responses are JSON, we do support JSONP with the callback query parameter. Every response in the API is returned in a common "wrapper" object, for easier and more consistent parsing.

所有API响应都是JSON,我们通过回调查询参数支持JSONP。 API中的每个响应都在一个通用的“包装器”对象中返回,以便更容易和更一致地解析。

So you should use dataType: 'json' rather than jsonp.

所以你应该使用dataType:'json'而不是jsonp。

You should also upgrade to the 2.1 API, the 1.x API has been deprecated for 6 months.

您还应该升级到2.1 API,1.x API已被弃用6个月。

#1


0  

Right now the application is returning Content-Type: application/json.

现在,应用程序返回Content-Type:application / json。

You can fix this by overriding the callback function name to jsonp which will tell the server to return Content-Type: application/javascript instead:

您可以通过将回调函数名称覆盖到jsonp来解决此问题,这将告诉服务器返回Content-Type:application / javascript:

$(document).ready(function () {
    $.ajax({
        url: 'http://api.*.com/1.1/tags/php/top-answerers/month',
        dataType: 'jsonp',
        jsonp: 'jsonp',
        success: function (data) {
            alert(data.top_users.length + ' users retrieved.');
        }
    });
});

Info on jsonp ajax setting:

有关jsonp ajax设置的信息:

jsonpString

Override the callback function name in a jsonp request. This value will be used instead of 'callback' in the 'callback=?' part of the query string in the url. So {jsonp:'onJSONPLoad'} would result in 'onJSONPLoad=?' passed to the server. As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }

覆盖jsonp请求中的回调函数名称。在'callback =?'中将使用此值代替'callback' url中查询字符串的一部分。所以{jsonp:'onJSONPLoad'}会导致'onJSONPLoad =?'传递给服务器。从jQuery 1.5开始,将jsonp选项设置为false可防止jQuery将“?callback”字符串添加到URL或尝试使用“=?”转型。在这种情况下,您还应该显式设置jsonpCallback设置。例如,{jsonp:false,jsonpCallback:“callbackName”}

#2


0  

From https://api.stackexchange.com/docs

All API responses are JSON, we do support JSONP with the callback query parameter. Every response in the API is returned in a common "wrapper" object, for easier and more consistent parsing.

所有API响应都是JSON,我们通过回调查询参数支持JSONP。 API中的每个响应都在一个通用的“包装器”对象中返回,以便更容易和更一致地解析。

So you should use dataType: 'json' rather than jsonp.

所以你应该使用dataType:'json'而不是jsonp。

You should also upgrade to the 2.1 API, the 1.x API has been deprecated for 6 months.

您还应该升级到2.1 API,1.x API已被弃用6个月。