开发者

jsonpCallback function not working

UPDATE 1:

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

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 success and the second showing jsonpCallback. But I am only getting the first alert success. Why is the second alert not showing up?


You should change:

jsonp: 'callback',

to

jsonp: false

to override the default callback value.

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


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.php does not resolve for me.


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


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

here it goes, i am using php 5.3 and 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');
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜