【发布时间】:2011-04-15 23:14:18
【问题描述】:
我正在使用 php/ajax 提交表单而无需刷新页面。这是我的文件-
coupon.js
jQuery(document).ready(function(){
jQuery(".appnitro").submit( function(e) {
$.ajax({
url : "sms.php",
type : "post",
dataType: "json",
data : $(this).serialize(),
success : function( data ) {
for(var id in data) {
jQuery('#' + id).html( data[id] );
}
}
});
//return false or
e.preventDefault();
});
});
短信.php
<?php
//process form
$res = "Message successfully delivered";
$arr = array( 'mess' => $res );
echo json_encode( $arr );//end sms processing
unset ($_POST);
?>
这是我的 html 页面的代码 -
<form id="smsform" class="appnitro" action="sms.php" method="post">
...
</form>
<div id="mess" style="background:green;"></div>
现在不是通过 ajax 提交表单而不刷新页面,而是页面被重定向到
baseurl/sms.php,页面上唯一可见的是
{"mess":"Message successfully delivered"}
我的猜测是 php 脚本没有成功返回到 jquery,因此 sms.php 的最后一部分中的回显正在显示。 我应该如何使 php 脚本成功返回?
关于如何调试的任何想法。我已尝试在 coupon.js 末尾使用 return false,但没有结果。
当我点击提交萤火虫给出以下结果 -
POST http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php
404 Not Found 1.29s `jquery.min.js (line 130)`
回应
Firebug needs to POST to the server to get this information for url:
http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php
This second POST can interfere with some sites. If you want to send the POST again, open a new tab in Firefox, use URL 'about:config', set boolean value 'extensions.firebug.allowDoublePost' to true
This value is reset every time you restart Firefox This problem will disappear when https://bugzilla.mozilla.org/show_bug.cgi?id=430155 is shipped
【问题讨论】:
-
只是出于好奇,
unset ($_POST);的意义何在? -
取消设置
$_POST只会有意义(a)请求将花费很长时间,(b)$_POST数组很大并且(c)即使那样它也是一个微型优化和/或不应该在 HTTP 请求中处理的事情。