Ok, I'm not 100% sure what I'm doing wrong here. Maybe I just need a second pair of eyes to reveal the error of my ways. I'm sending a JSONP request using jQuery from one domain (https://customerhub.net) to a PHP script on my domain (https://dankennedy.com) and getting a response from that script. I know the request is working because, for testing purposes, I'm printing all my input to a file before sending the response and the data is getting through. However, for whatever reason, the callback function isn't working.
Here's the jQuery code:
jQuery('#form1').validationEngine({
failure: false,
success: function(){
var url = 'https://dankennedy.com/test2.php?jsoncallback=?';
jQuery.getJSON(url, jQuery('#form1').serialize(), function(){
alert('I ran... for once');
});
return false;
}
});
Here's the PHP code:
$callback = $_REQUEST['jsoncallback'];
$myFile = "info.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$data = '';
foreach( $_REQUEST as $key => $value )
$data .= ", `".$key."` = \"".$value."\"";
$stringData = 'Array: ' . substr( $data, 1 );
fwrite($fh, $stringData);
$response = array( 'msg' => 'SUCCESS' );
$fullResponse = $callback.'('.json_encode($response).')';
fwrite($fh, "\n".$fullResponse );
fclose($fh);
header( 'Content-type: text/plain' );
echo $fullResponse;
Eventually I'll do more with the code and have my PHP script access my database and send back some meaningful data and have my jQuery function do some processing on that data. For now, I'm just printing stuff out and starting small to make sure the exchange of information over JSONP is working.
Everything looks good. I'm following the code in Firebug too and seeing how the code is being processed. The request is being made but the alert statement inside isn't running.
This is what the file being written to is printing out:
Array 5: `jsoncallback` = "jsonp1277479829087", `id` = "", `type` = "blog", `task` = "add", `title` = "Brace Ford", `date` = "June 25, 2010 11:30 AM", `author` = "", `teaser` = "Teaser", `body` = "Body", `download` = "", `comments` = "", `__utma` = "119149980.718135870.1277328038.1277328038.1277386321.2", `__utmz` = "119149980.1277328038.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)", `SESS6c0bc6ae4aee7e8c564bc45425742fc0` = "tpc7lmtv551kg1vpuvo2c9efi1", `__utmv` = "119149980.authenticated user,s1a", `mifge` = "DKFrontPage", `__utmc` = "119149980"
jsonp1277479829087({"msg":"SUCCESS"})
Any idea what my grievous error is?