我使用php / ajax提交没有页面刷新的表单。这是我的文件 -
coupon.js
jQuery(document).ready(function(){
jQuery(".appnitro").submit( function(e) {
$.ajax({
url : "http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/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();
});
});
sms.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="http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php" method="post">
...
</form>
<div id="mess" style="background:green;"></div>
现在,当我点击提交按钮时,没有任何反应,并且在控制台面板下显示了firebug -
POST http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php
404 Not Found 1.29s `jquery.min.js (line 130)`
Response
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
当我将'extensions.firebug.allowDoublePost'设置为true时,会显示以下结果 -
POST http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street/sms.php
404 Not Found 1.29s `jquery.min.js (line 130)`
Response -
{"mess":"Message successfully delivered"}
CaN有人帮我修复了404找不到的firebug错误。为什么它旁边显示jquery.min.js (line 130)
?
P.S-不要担心http://174.132.194.155/~kunal17/devbuzzr/wp-content/themes/street这是我的基本网址
答案 0 :(得分:0)
您可能想尝试在$ .ajax调用之前放置e.preventDefault()语句。
修改强>
我的x.html,对应于您的HTML页面
<!DOCTYPE html>
<html>
<head>
<title>x</title>
<script
type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script type="text/javascript" src="/so/x.js"></script>
</head>
<body>
<form id="smsform" class="appnitro" action="/so/x.php">
<input type="text" name="zz">
<input type="submit">
</form>
<div id="mess" style="background:green;"></div>
</body>
</html>
我的x.js,对应于您的coupon.js
jQuery(document).ready(function(){
jQuery(".appnitro").submit( function(e) {
e.preventDefault();
$.ajax({
url : "/so/x.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();
});
});
我的x.php,对应你的sms.php
<?php
$res = "Message successfully delivered.";
$arr = array('mess'=>$res);
echo json_encode($arr);
unset($_POST);
?>
这实际上适用于我的环境,虽然我没有剩余的HTML标记或其他PHP表单处理代码。 “消息已成功发送”。在输入字段的正下方显示为绿色。
答案 1 :(得分:0)
当在Ajax调用内部时,这指的是您需要执行此操作的Ajax对象
var __this = this;
在进入Ajax调用之前,它将是
data : __this.serialize()
或者在Google的Ajax调用中查找上下文的使用。或者在进入Ajax调用之前将数据序列化为变量。