views:

66

answers:

2

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?

A: 

You won't be able to debug the getJSON success function code in FireBug, but that's expected. What you should see instead (if you look at the source in FireBug) is a script tag injected in the html head. Does that happen correctly? And try doing this to see if the response gets through - return a string such as "alert('test')" from the server:

$fullResponse = 'alert("test")';
echo $fullResponse;

Does the alert fire?

Yakimych
Yea, I didn't think Firebug would reveal much in the stack but it would at least let me trace the progression of the code and let me know that it attempted to go into the "on success" function or not.Could try that. Would sending just "alert('test')" from the php script to the requesting page show up? I don't need to encode it in JSON format, like..$callback.'( "alert('test');" )';I could give that a try. Thanks for the suggestion!
TJ Kirchner
Great you've solved it, congrats! Just a quick reply to your comments. The reason why alert('test'); should fire is that what actually happens is that a <script> tag in generated and inserted into the head. It's source is the Url you specify in your call, so if you return any javascript command, it will be treated the same as referencing a *.js file containing that command - such as alert('test'); in our case. And when the script gets injected, the js code gets run. But that's mostly good for quick tests and debugging, congrats again on solving it!
Yakimych
A: 

I figured out what the problem was. For whatever reason, the callback I should have been using was "JSONcallback" and not "jsoncallback". Given Charles idea' and a suggestion I found on another forum about capitalizing the "JSON", I figured I'd give it a try. Worked like a charm. Thanks for your help guys!

TJ Kirchner