Javascript JSONP 请求返回错误:“Uncaught SyntaxError: Unexpected token :”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6046008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:00:50  来源:igfitidea点击:

JSONP request returning error: "Uncaught SyntaxError: Unexpected token :"

javascriptajaxjquerystackexchange-api

提问by theabraham

So I'm trying to make a request to the Stack Exchange API with the following jQuery code:

因此,我尝试使用以下 jQuery 代码向 Stack Exchange API 发出请求:

$.ajax({                                                                                                                                                                                                        
    type: 'POST',                                                                                                                                                                                                 
    url: 'http://api.stackoverflow.com/1.1/stats',                                                                                                                                              
    dataType: 'jsonp',                                                                                                                                                                                                
    success: function() { console.log('Success!'); },                                                                                                                                                                                       
    error: function() { console.log('Uh Oh!'); }                                                                                                                                                              
});   

But when I open the file on my machine, in either FireFox or Chrome, and make the request, I get this error:

但是当我在我的机器上打开文件时,在 FireFox 或 Chrome 中,并发出请求,我收到此错误:

Resource interpreted as Script but transferred with MIME type application/json.
Uncaught SyntaxError: Unexpected token :
Uh Oh!

I don't have a clue what's going on. I know the Stack Exchange API Gzips its responses, would this cause any trouble?

我不知道发生了什么。我知道 Stack Exchange API Gzip 对其响应进行压缩,这会造成什么麻烦吗?

采纳答案by lonesomeday

You have to set an unconventional parameter to get the SO API to work. Rather than the conventional callback, you need to pass a jsonpparameter.

您必须设置一个非常规参数才能使 SO API 工作。而不是传统的callback,您需要传递一个jsonp参数。

Furthermore, you can't do POSTwith JSONP.

此外,您不能POST使用 JSONP。

$.ajax({                                                                                                                                                                                                        
    type: 'GET',                                                                                                                                                                                                 
    url: 'http://api.stackoverflow.com/1.1/stats',                                                                                                                                              
    dataType: 'jsonp',                                                                                                                                                                                                
    success: function() { console.log('Success!'); },                                                                                                                                                                                       
    error: function() { console.log('Uh Oh!'); },
    jsonp: 'jsonp'                                                                                                                                                
});


It is not possible to do cross-domain AJAX using the conventional XMLHTTPRequest. This is for security reasons (it's call the same-origin policy).

使用传统的 XMLHTTPRequest 无法进行跨域 AJAX。这是出于安全原因(称为同源策略)。

There is a workaround. scripttags are not subject to this restriction. This means that you can insert a scripttag into the document that calls a URL. If you define a globally-accessible function in your script and tell the remote server what that function is called, the server can pass code that wraps the data to be sent in a call to that function.

有一个解决方法。 script标签不受此限制。这意味着您可以将一个script标签插入到调用 URL 的文档中。如果您在脚本中定义了一个全局可访问的函数并告诉远程服务器该函数的调用内容,则服务器可以传递包含要在调用该函数时发送的数据的代码。

The difficulty you had here is with the StackOverflow API. Conventionally, you would use the callbackargument in your request, to tell the server what your function is called. However, StackOverflow's API asks you to use the jsonpparameter instead.

您在这里遇到的困难在于 StackOverflow API。通常,您会callback在请求中使用参数来告诉服务器您的函数被调用了什么。但是,StackOverflow 的 API 要求您改用jsonp参数。

回答by M?rre

Try this URL: http://api.stackoverflow.com/1.1/stats?jsonp=callme

试试这个网址:http: //api.stackoverflow.com/1.1/stats?jsonp=callme

"callme" is the name of your callback function - in your GLOBAL NAMESPACE (window object).

“callme”是您的回调函数的名称 - 在您的 GLOBAL NAMESPACE(窗口对象)中。

By the way, if you are running Firefox and have the JSONView add-on installed you can test the above URL (and yours for comparison) directly.

顺便说一句,如果您正在运行 Firefox 并安装了 JSONView 附加组件,您可以直接测试上述 URL(以及您的 URL 以进行比较)。

Result from calling the URL:

调用 URL 的结果:

callme({
  "statistics": [
...
  ]
})