3

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?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

4 Answers4

6

You should change:

jsonp: 'callback',

to

jsonp: false

to override the default callback value.

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

George Cummins
  • 28,485
  • 8
  • 71
  • 90
3

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.

Zhami
  • 19,033
  • 14
  • 48
  • 47
  • the callback gets added to the url automatically afaik. – oshirowanen Jun 02 '11 at 14:06
  • Please see update 1. Also, remote_host was done to hide my website/company name. – oshirowanen Jun 02 '11 at 14:26
  • Correct, the callback name gets added automatically; set: "jsonp:" to false, and set "jsonpCallback:" as is required by the host server. jQuery handles the response. The callback name you supply is *not* invoked as a function. You pick up the JSON data in "success" callback. – Zhami Jun 02 '11 at 16:45
0

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

Simon
  • 2,810
  • 2
  • 18
  • 23
  • As of jQuery 1.5, a function can be supplied as the jsonpCallback param of the $.ajax method. This function is not invoked upon receipt of the response from the XHR request, but prior to execution of the request, and must return a string (which will be used for the callback arg in the URL). – Zhami Jun 02 '11 at 14:13
-1

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');
    });
});
Talha
  • 1,546
  • 17
  • 15