使用 JQuerys .post() 没有响应
我有一个非常简单的表单来更新用户的状态消息。表单确实有效,但我没有收到任何响应(使用 firebug 提交后的响应为空)。
这是 html/js:
<form id="StatusUpdateForm" method="post" action="http://www.url.com/process.php">
<input name="StatusUpdate" type="text" id="StatusUpdate" />
<input type="hidden" name="userid" value="<?=$_SESSION['user']['id']?>" />
<input type="submit" value="Update your status" />
</form>
<div id="results"></div>
<script>
/* attach a submit handler to the form */
$("#StatusUpdateForm").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
StatusUpdate = $form.find( 'input[name="StatusUpdate"]' ).val(),
userid = $form.find( 'input[name="userid"]' ).val(),
url = $form.attr( 'action' );
/* Send the data using post and put the results in a div */
$.post( url , $("#StatusUpdateForm").serialize(),
function( data ) {
$('#results').html(data);
}
);
});
</script>
这是 process.php:
if($_POST['StatusUpdate']&&$_POST['userid']){
if( StatusMessage::set($_POST['userid'], $_POST['StatusUpdate']) ){
echo "<div id='content'>Comment updated!</div>";
} else {
echo "<div id='content'>Problem updating comment. Try again.</div>";
}
}
I have a very simple form for updating a user's status message. The form does work, but I am not getting any response (using firebug the response after submit is empty).
Here is the html/js:
<form id="StatusUpdateForm" method="post" action="http://www.url.com/process.php">
<input name="StatusUpdate" type="text" id="StatusUpdate" />
<input type="hidden" name="userid" value="<?=$_SESSION['user']['id']?>" />
<input type="submit" value="Update your status" />
</form>
<div id="results"></div>
<script>
/* attach a submit handler to the form */
$("#StatusUpdateForm").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
StatusUpdate = $form.find( 'input[name="StatusUpdate"]' ).val(),
userid = $form.find( 'input[name="userid"]' ).val(),
url = $form.attr( 'action' );
/* Send the data using post and put the results in a div */
$.post( url , $("#StatusUpdateForm").serialize(),
function( data ) {
$('#results').html(data);
}
);
});
</script>
Here is process.php:
if($_POST['StatusUpdate']&&$_POST['userid']){
if( StatusMessage::set($_POST['userid'], $_POST['StatusUpdate']) ){
echo "<div id='content'>Comment updated!</div>";
} else {
echo "<div id='content'>Problem updating comment. Try again.</div>";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
return
更改为echo
。如果它没有输出任何东西,就不会发送任何响应!为了额外确保它正在执行,请添加一个错误回调函数:
或者(首选):
Change
return
toecho
. If it's not outputting anything there won't be any response to send!To be extra sure it's executing, add an error callback function:
Or (preferred):
调用ajax时不能使用绝对路径。 即使,如果您没有调用外部网址。
将 http://www.url.com/process.php 更改为 process.php
You cannot use absolute paths when calling ajax. Even if you are not calling an outside url.
change
http://www.url.com/process.php
toprocess.php