JSONP 请求的问题

发布于 2024-09-07 01:04:01 字数 2070 浏览 1 评论 0原文

好吧,我不是 100% 确定我在这里做错了什么。也许我只是需要第二双眼睛来揭露我的错误。我正在使用 jQuery 从一个域 (https://customerhub.net) 向 PHP 脚本发送 JSONP 请求我的域名 (https://dankennedy.com) 并从该脚本获取响应。我知道该请求正在运行,因为出于测试目的,我在发送响应之前将所有输入打印到文件中,并且数据正在通过。但是,无论出于何种原因,回调函数都不起作用。

这是 jQuery 代码:

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;
 }
});

这是 PHP 代码:

$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;

最终,我将使用该代码执行更多操作,让我的 PHP 脚本访问我的数据库并发回一些有意义的数据,并让我的 jQuery 函数对这些数据进行一些处理。目前,我只是打印一些内容并从小处开始,以确保通过 JSONP 进行的信息交换正常工作。

一切看起来都不错。我也在关注 Firebug 中的代码,看看代码是如何处理的。正在发出请求,但内部的警报语句未运行。

这就是正在写入的文件打印出来的内容:

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"})

知道我的严重错误是什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守望孤独 2024-09-14 01:04:01

您将无法在 FireBug 中调试 getJSON success 函数代码,但这是预期的。相反,您应该看到的(如果您查看 FireBug 中的源代码)是注入到 html head 中的脚本标记。这种情况正确吗?
尝试执行此操作以查看响应是否通过 - 从服务器返回诸如“alert('test')”之类的字符串:

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

警报是否触发?

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?

我们的影子 2024-09-14 01:04:01

我明白问题出在哪里了。无论出于何种原因,我应该使用的回调是“JSONcallback”而不是“jsoncallback”。鉴于 Charles 的想法和我在另一个论坛上找到的关于大写“JSON”的建议,我想我应该尝试一下。工作起来就像一个魅力。谢谢你们的帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文