Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么在POST请求后显示页面?_Javascript_Jquery_Post - Fatal编程技术网

Javascript 为什么在POST请求后显示页面?

Javascript 为什么在POST请求后显示页面?,javascript,jquery,post,Javascript,Jquery,Post,很抱歉这个奇怪的标题。我有以下代码测试jquery的post()函数: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" /> <title>Untitled Document</title> <script type="text/j

很抱歉这个奇怪的标题。我有以下代码测试jquery的post()函数:

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" /> <title>Untitled Document</title> <script type="text/javascript" src="../../jquery/jquery-1.4.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("form#f").submit(function(){ var result = ""; $.ajax({ async: false, type: "POST", url: "test.php", dataType: "script", success: function(data){ result = data; } }); alert(result); }); } </script> </head> <body> <form method="post" action="test.php" id="f"> <input type="text" name="name" /> <input type="submit"/> </form> </body> </html> 无标题文件 $(文档).ready(函数(){ $(“表格f”)。提交(函数(){ var结果=”; $.ajax({ async:false, 类型:“POST”, url:“test.php”, 数据类型:“脚本”, 成功:功能(数据){ 结果=数据; } }); 警报(结果); }); } …这是test.php实现:

<?php header('Content-Type: text/plain'); echo "hello " . $_POST['name']; ?>
为什么在submit()函数中显示的是页面而不是警报对话框中显示的数据?很抱歉,我知道这是一个n00b问题。我不太擅长Javascript。

在提交处理程序末尾返回false;。否则,您不会阻止正常提交(post to test.php)


在提交处理程序的末尾执行
return false;
。否则不会阻止正常提交(post to test.php)


返回false的另一种方法是使用jQuery的:


返回false的另一种方法是使用jQuery的:


@Jairo我试过你的原始代码,它使用CDN工作

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>

@Jairo我尝试了您的原始代码,它使用CDN工作

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>


尝试在分配给提交操作的函数末尾添加
返回false;
。我相信这将覆盖默认行为,即实际将表单提交到操作url。将
更改为
尝试在分配给提交操作的函数末尾添加
返回false;
我相信这将覆盖默认行为,即实际将表单提交到操作url。将你的
更改为
都德!你从我的评论中窃取了你的答案!只是开玩笑:-P+1@Endophage.哈哈……我想你误解了它的工作原理:)@Jairo。什么“不起作用”?它还在发布表单,还是你的JS控制台出现了错误?伙计!你从我的评论中偷走了你的答案!开玩笑吧+1@Endophage. 哈哈。。。我想你误解了它的工作原理:)@Jairo。什么“不起作用”?它是否仍在发布表单,或者您的JS控制台中是否出现错误?我认为这不起作用的原因是它没有将
警报
移动到成功回调中
event.preventDefault
是一个比
return false
更好的解决方案,因为如果事件处理程序中存在未捕获的错误,它仍然会取消事件。我认为这不起作用的原因是它没有将
警报
移动到成功回调中<但是,代码>事件.preventDefault是比返回false更好的解决方案,因为如果事件处理程序中存在未捕获的错误,它仍然会取消事件。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>