dqwd71332
2010-06-25 15:52
浏览 37

JSONP请求的麻烦

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, "
".$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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongnuo4594 2010-06-30 15:40
    已采纳

    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!

    点赞 评论
  • duandong1869 2010-06-29 08:49

    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?

    点赞 评论

相关推荐 更多相似问题