AJAX POST请求失败到同一页面(带示例)

代码

####### The name of this file is 'test_ajaxfn.php' ####### <?php if($_POST['var']) { $result = $_POST['var']; echo $result.' is successfully passed to the same page using Ajax Post. :)'; } else { echo 'There is no POST variable passed to the same page. :( '; } echo '
Above indicates the ajax post function \'.\' '; ?> Untitled Document $(document).ready(function() { $('#clickdiv').click(function() { var sth = "value to send to PHP"; $.post( 'test_ajaxfn.php', {"var":"1234567"}, function(){ alert('success'); $('#clickdiv').text('the POST variable should be posted.'); } ); }); });

描述

我想将POST变量传递到同一页面并使用AJAX加载它。 但是,我没有这样做。 我无法使用POST方法将“var”传递给$ _POST [‘var’]。 因此我无法显示以下内容:echo $ result。’ 使用Ajax Post成功传递到同一页面。 :)“;

我做错了什么?

参考

我实际上是试图解决这个问题: 使用jQuery ajax.get计算输入地址的谷歌距离和MySQL服务器的所有地址现在我简化了我的问题,但它仍然无效。 请帮忙 :(

如果将其发布到同一页面,则服务器会创建一个新进程来处理请求。 你没有看到任何输出的原因是因为服务器正在查看文件的post,启动实例并执行php脚本,但这与你的php线程不同。 将脚本更改为此以查看您的成功文本。