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:


 $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


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


 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?

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.

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

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.