I have this PHP:
$response = 'success';
$response = json_encode($response);
echo $_GET['callback'] . '('.$response.')';
and this jQuery:
$.ajax({url: "/*url*/?invite="+getinvite+"&api="+api, dataType:"jsonp",
success: function(response){/*do stuff*/}});
On XAMPP on my laptop it was working great. Then when I put it on my server it gives me this error: Undefined index: callback
.
Why is it not letting me set $_GET['callback']
? and how would this problem be fixed?
You aren't setting a parameter called 'callback' and so it isn't set in the $_GET variables.
You could fix the error message by doing:
$callback = "";
if(array_key_exists('callback', $_GET) == TRUE){
$callback = $_GET['callback'];
}
However I don't think you may have misunderstood the .ajax call in jQuery - the javascript function that is called on success is not passed to the server; it is only held inside jQuery.
To make your ajax call simple, use getJson method designed specifically for JSONP response.
http://api.jquery.com/jQuery.getJSON/
This will automatically create a callback function name which will be passed as parameter to your jsonp api . example from the page :
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
{
tags: "cat",
tagmode: "any",
format: "json"
},
function(data) {
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#images");
if ( i == 3 ) return false;
});
});