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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。