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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序