jQuery JSONP 调用显示“Uncaught SyntaxError: Unexpected token :”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19165925/
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-26 23:20:40  来源:igfitidea点击:

JSONP call showing "Uncaught SyntaxError: Unexpected token : "

jqueryjsonp

提问by Katakam Nikhil

Here is my code

这是我的代码

$.ajax({
        url: 'https://api.flightstats.com/flex/schedules/rest/v1/json/flight/AA/100/departing/2013/10/4?appId=19d57e69&appKey=e0ea60854c1205af43fd7b1203005d59&callback=?',
        dataType: 'JSONP',
        jsonpCallback: 'jsonCallback',
        type : 'GET',
        async: false,
        crossDomain: true,
        success: function(data) {
            console.log(data);
        }
    });

What am I doing wrong? should I add or change anything in here? Any help would be appreciated. Thanks

我究竟做错了什么?我应该在这里添加或更改任何内容吗?任何帮助,将不胜感激。谢谢

回答by Jason P

Working fiddle:

工作小提琴:

http://jsfiddle.net/repjt/

http://jsfiddle.net/repjt/

$.ajax({
    url: 'https://api.flightstats.com/flex/schedules/rest/v1/jsonp/flight/AA/100/departing/2013/10/4?appId=19d57e69&appKey=e0ea60854c1205af43fd7b1203005d59',
    dataType: 'JSONP',
    jsonpCallback: 'callback',
    type: 'GET',
    success: function (data) {
        console.log(data);
    }
});

I had to manually set the callback to callback, since that's all the remote service seems to support. I also changed the url to specify that I wanted jsonp.

我不得不手动将回调设置为callback,因为这就是远程服务似乎支持的全部内容。我还更改了 url 以指定我想要 jsonp。

回答by Johan

You're trying to access a JSON, not JSONP.

您正在尝试访问 JSON,而不是 JSONP。

Notice the difference between your source:

请注意您的来源之间的区别:

https://api.flightstats.com/flex/schedules/rest/v1/json/flight/AA/100/departing/2013/10/4?appId=19d57e69&appKey=e0ea60854c1205af43fd7b1203005d59&callback=?

https://api.flightstats.com/flex/schedules/rest/v1/json/flight/AA/100/departing/2013/10/4?appId=19d57e69&appKey=e0ea60854c1205af43fd7b1203005d59&callback=

And actual JSONP (a wrapping function):

和实际的 JSONP(一个包装函数):

http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=processJSON&tags=monkey&tagmode=any&format=json

http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=processJSON&tags=monkey&tagmode=any&format=json

Search for JSON + CORS/Cross-domain policy and you will find hundreds of SO threads on this very topic.

搜索 JSON + CORS/Cross-domain policy,你会发现数百个关于这个主题的 SO 线程。

回答by GiorgosBarkos

I run this

我运行这个

var data = '{"rut" : "' + $('#cb_rut').val() + '" , "email" : "' + $('#email').val() + '" }';
var data = JSON.parse(data);

$.ajax({
    type: 'GET',
    url: 'linkserverApi',
    success: function(success) {
        console.log('Success!');
        console.log(success);
    },
    error: function() {
        console.log('Uh Oh!');
    },
    jsonp: 'jsonp'

});

And edit header in the response

并在响应中编辑标题

'Access-Control-Allow-Methods' , 'GET, POST, PUT, DELETE'

'Access-Control-Max-Age' , '3628800'

'Access-Control-Allow-Origin', 'websiteresponseUrl'

'Content-Type', 'text/javascript; charset=utf8'

“访问控制允许方法”、“获取、发布、放置、删除”

“访问控制最大年龄”,“3628800”

'Access-Control-Allow-Origin', 'websiteresponseUrl'

'内容类型', '文本/javascript; 字符集=utf8'