jQuery jsonpCallback 函数不起作用

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

jsonpCallback function not working

jqueryjsonp

提问by oshirowanen

UPDATE 1:

更新1:

I've just upgraded from jquery 1.4.4 to 1.6.1. How does that effect the script in the original question?

我刚刚从 jquery 1.4.4 升级到 1.6.1。这对原始问题中的脚本有何影响?

ORIGINAL QUESTION:

原问题:

Just as I test, I did:

就像我测试一样,我做了:

$(document).ready(function() {
    get_jsonp_feed();

    function get_jsonp_feed() {
        $.ajax({
            url: 'http://www.remote_host.co.uk/feed.php',
            type: 'GET',
            dataType: 'jsonp',
            jsonp: 'callback',
            jsonpCallback: 'jsonpCallback',
            error: function(xhr, status, error) {
                alert("error");
            },
            success: function(jsonp) { 
                alert("success");
            }
        });
    }

    function jsonpCallback(data){
        alert("jsonpCallback");
    }
});

I was expecting to get 2 alerts, the first showing successand the second showing jsonpCallback. But I am only getting the first alert success. Why is the second alert not showing up?

我期待收到 2 个警报,第一个显示success和第二个显示jsonpCallback。但我只收到第一个警报success。为什么第二个警报没有出现?

回答by George Cummins

You should change:

你应该改变:

jsonp: 'callback',

to

jsonp: false

to override the default callback value.

覆盖默认回调值。

See: http://api.jquery.com/jQuery.ajax/

请参阅:http: //api.jquery.com/jQuery.ajax/

回答by Zhami

George is correct, set the jsonp param to false -- as of jQuery 1.5 (so, how you set this up is jQuery version dependent). I don't believe that your supplied callback name is invoked as a function (rather, it is the name provided in the URL presented to the server). If you are getting success, then you have received the data. Curious: do you have a hosts entry set up for dev, because I tried to do some testing, and http://www.remote_host.co.uk/feed.phpdoes not resolve for me.

George 是正确的,将 jsonp 参数设置为 false——从 jQuery 1.5 开始(因此,如何设置取决于 jQuery 版本)。我不相信您提供的回调名称是作为函数调用的(相反,它是提供给服务器的 URL 中提供的名称)。如果你成功了,那么你已经收到了数据。好奇:您是否为 dev 设置了主机条目,因为我尝试进行了一些测试,而http://www.remote_host.co.uk/feed.php无法为我解析。

回答by simonlchilds

I think you need to change the jsonpCallBack:'jsonpCallback'bit to jsonpCallBack: function() { alert('boo'); }

我认为您需要将jsonpCallBack:'jsonpCallback'位更改为jsonpCallBack: function() { alert('boo'); }

回答by Talha

To all my friends who are having problem with PHP + JQuery + JSONP

致所有对 PHP + JQuery + JSONP 有问题的朋友

here it goes, i am using php 5.3 and Jquery 1.10

好了,我正在使用 php 5.3 和 Jquery 1.10

$('#button_submit2').click(function () {
    prevent_caching = (new Date()).getTime();
    $.ajax({
        type: 'GET'
        , url: "http://yoururl.com/webservice.php"
        , dataType: 'jsonp'     //Besides plain xml, the dataType can be html, json, jsonp, script, or text.
        , jsonp: 'callback'     //this will be added in the query as parameter
        , jsonpCallback: 'jsonp_reply'  //this is what ajax call is expecting json to be encapsulated ine i.e. json_reply(JSON_RESPONSE)
        , data: {
            uniq_val: prevent_caching
            , method_name: "get_all_tasks"
            , format: 'jsonp'
        }
        , cache: false
        , async: false
    })
    .success(function (rsp) {
        console.log('success'+rsp);
    })
    .fail(function (xhr, error, thrownError) {
        console.log('fail  status:[' + xhr.status + ']  error:[' + thrownError + ']');
    })
    .always(function () {
        console.log('complete');
    });
});