JSONP请求返回错误:"未捕获的SyntaxError:意外的标记:" [英] JSONP request returning error: "Uncaught SyntaxError: Unexpected token :"

查看:464
本文介绍了JSONP请求返回错误:"未捕获的SyntaxError:意外的标记:"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想提出一个请求有以下jQuery的code堆栈交易所API:

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

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

但是,当我打开我的机器上的文件,在任何Firefox或Chrome,并发出请求,我​​得到这个错误:

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

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

我没有一个线索,这是怎么回事。我知道堆栈交易所API的gzip的反应,这会造成什么麻烦?

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

推荐答案

您必须设置一个非常规的参数,以获得SO API来工作。而不是传统的回调,你需要传递一个 JSONP 参数。

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

此外,你不能做 POST 与JSONP。

Furthermore, you can't do POST with 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'                                                                                                                                                
});


这是不可能使用传统XMLHTT prequest做跨域Ajax。这是出于安全的原因(这是叫同源策略)。


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).

有一种变通方法。 剧本标记不受此限制。这意味着,你可以插入一个剧本标记成调用一个URL文件。如果你定义脚本中的全局可访问的功能,并告诉远程服务器是什么函数被调用时,服务器可以通过code,它封装了数据在调用该函数来发送。

There is a workaround. script tags are not subject to this restriction. This means that you can insert a script tag 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.

你有这里的困难与计算器API。通常,你可以使用回调参数在您的要求,要告诉你的函数被调用的服务器。然而,计算器的​​API要求您使用 JSONP 参数来代替。

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

这篇关于JSONP请求返回错误:"未捕获的SyntaxError:意外的标记:"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆