Javascript jqueryajax之后有什么功能?
我目前正试图让我的Ajax将一些信息从HTML表单发布到PHP表单中 现在,我已经编写了不同的代码,应该在“ajax.comment.php”页面上打印出来,告诉大家发生了什么 比如它成功与否 现在我想制作ajax,检查打印的html是什么。 然后再处理这件事。。比如如果(打印的HTML==“1”){那么做点什么} 我该怎么做 我的以下javascript是:Javascript jqueryajax之后有什么功能?,javascript,ajax,jquery,Javascript,Ajax,Jquery,我目前正试图让我的Ajax将一些信息从HTML表单发布到PHP表单中 现在,我已经编写了不同的代码,应该在“ajax.comment.php”页面上打印出来,告诉大家发生了什么 比如它成功与否 现在我想制作ajax,检查打印的html是什么。 然后再处理这件事。。比如如果(打印的HTML==“1”){那么做点什么} 我该怎么做 我的以下javascript是: var name = document.comment.name.value; var email = document.comment
var name = document.comment.name.value;
var email = document.comment.email.value;
var website = document.comment.website.value;
var message = document.comment.message.value;
var id = document.comment.id.value;
$.ajax({
type: "POST",
url: "ajax.addcomment.php",
data: "name="+name+"&email="+email+"&website="+website+"&message="+message+"&id="+id,
beforeSend: function() {
// Action before sending data
},
success: function(returned_html) {
// Action after sending data, where the returned_html var is the returned html text by the php
}
});
在
success
函数中,尝试以下操作:
success: function(returned_html) {
var the_result = $.trim(returned_html);
if(the_result == '1')
{
// Do whatever you wanted here.
}
else
{
// Do something else here...
}
}
你差不多完成了。只需在您已经实现的
success
回调中执行您想要的操作
顺便说一句,我已经序列化了您的表单,这样您就不需要单独获取值(除非您想使用它们)
您想要返回的不是
吗?ajax调用或任何http post/get都不会返回整数;)请在回答前3分钟查看答案,了解正确的格式。对serialize()
的调用很好。这不是身份检查!应该有效:s。但是是的,修剪可能会更好。只需提到javascript的常见用法-您可能希望执行_result=='1'以确保结果同时为1和字符串-通常最好尽早显式显示,以防稍后要对_结果执行某些操作,而它的行为与您预期的不一样,运行$.trim()
将始终返回字符串。
$.ajax({
type: "POST",
url: "ajax.addcomment.php",
data: $('#comment').serialize(),
success: function(returned_html) {
if(returned_html == 1){
//lets do our thing
} else {
//lets do other things
}
});