javascript 带有 jsonp 的 jQuery .ajax() 不调用成功回调函数

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

jQuery .ajax() with jsonp not invoking success callback function

javascriptjqueryajaxjsonjsonp

提问by Casey Flynn

I have a facebook iframe application that makes a cross domain request to my server and requests data in JSONP format. This is my client side code:

我有一个 facebook iframe 应用程序,它向我的服务器发出跨域请求并以 JSONP 格式请求数据。这是我的客户端代码:

jQuery.ajax({
                url: '***',
                type: 'post',
                data: {
                    method: 'set_user_prizes'
                },
                dataType: 'jsonp',
                jsonp: false,
                jsonpCallbackString: 'callback123',
                success: function(data, textStatus, jqXHR){
                    console.log('success_function');
                    console.log(data);
                }
});

The problem is my success callback method isn't being invoked and I'm not sure why. Using Firebug I can see my server's response:

问题是我的成功回调方法没有被调用,我不知道为什么。使用 Firebug 我可以看到我的服务器的响应:

callback123({"success":true,"associated_prizes":[{"prizes_id":"6"},{"prizes_id":"1"}]})

采纳答案by James

Remove the word Stringfrom the callback key as is illustrated in the following transformation. The value needs to remain a string.

String从回调键中删除单词,如下面的转换所示。该值需要保持为字符串。

Change:

改变:

jsonpCallbackString: 'callback123',

to

jsonpCallback: 'callback123',

回答by ThomasReggi

The correct answer is

正确答案是

jsonpCallback: 'callback123'