SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JSONP Callback not receiving data

    I have some code below which sends data to a server and then the server returns results in JSON however the callback for the function is not getting any data when I alert it is comes up as undefined. My code is below


    Here in my php web service I encode my array because thats the only way I can get the data from the datatype text column
    Code:
    Code:
     $posts[] = array_map('utf8_encode',$post);
    When I query the service directly I get this
    ([{"artist_id":"14","artist_name":"Nebula 868" etc. etc.
    My jQuery code is below and it works except for the callback
    Code:
    var durl =  "a valid url";
    $.ajax({
        type: "GET",
        url: durl,
        data:  "Pass="+avalidid,
        dataType: "jsonp",
        cache : false,
        jsonp : "onJSONPLoad",
        jsonpCallback: "callback",
        contentType: "application/json",
        crossDomain: "true",
        }).done(function(){ 
    
        }).fail(function(){ 
            $.mobile.changePage("errorpage");
        })
     });
    
    
    
    //callback function 
    function callback(rtndata) 
    { 
    alert(rtndata.name);
    }
    Here is the last few lines of the Web service that returns json

    Code:
     echo $_GET['onJSONPLoad'];
     echo "(" . json_encode($posts) . ")";
    when I try to alert the json within the callback I get undefined. No clue as to why any what that I can solve this issue?

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Typically you don't need to use the code you have setup above as its essentially doing what jQuery already does for you in the getJSON() method, see the following example which uses the flicker API as an example but it carries the same JSONp data return type.

    http://api.jquery.com/jQuery.getJSON/#entry-examples

    The only thing I can see that might make your current code fail is the callback name but I think using the getJSON() method will solve your current issue given you simply use ?jsoncallback=? and $_GET['jsoncallback'] which allows jQuery to worry about what the callback is doing instead of you which makes it more maintainable.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2011
    Location
    Trinidad and Tobago
    Posts
    194
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually found my error it seems the json being returned is in an array so I had to access it via an array index

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,600
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The title on this thread is misleading. You are not using JSONP at all - instead you are usingXMLlHttpRequest and JSON.

    JSONP is a cross domain alternative way of retrieving data from the server that doesn't use an XMLlHttpRequest at all. See http://javascriptexample.net/ajax07.php for an example of the code you'd use in JavaScript to implement JSONP calls.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •