有几个问题,首先是下面这个 var 字符串,第一个有效,但我需要让第二个有效,有一个 snytax 错误,因为我不确定如何编写它
var string = 'id='+ id ;
var string = 'id='+ id 'USER=1';
第二个; 下面的这个 ajax 调用,它发布到 delete.php 以删除评论,它有效,但我想添加一些错误处理。将它添加到后端后,如何让它在 jquery 中显示错误?
$.ajax({
type: "POST",
url: "delete.php",
data: string,
cache: false,
success: function(){
commentContainer.slideUp('slow', function() {$(this).remove();});
$('#load').fadeOut();
}
最佳答案
你缺少连接运算符和符号,我也不会使用“字符串”作为变量名:
var str = 'id='+ id + '&USER=1';
也就是说,'string' 不是 JS reserved word , 但似乎仍然是不好的做法(至少对我而言)。
您将“字符串”变量传递给 $.ajax 的是一个查询字符串,我建议阅读此内容以获取更多信息:
http://en.wikipedia.org/wiki/Query_string
关于错误处理的问题,一种方法是检查响应文本并采取相应措施,例如:
$.ajax({
type: "POST",
url: "delete.php",
data: string,
cache: false,
success: function(resp){
if(resp == 'error') { //get PHP to echo the string 'error' if something bad happened
alert('There was an error!');
} else {
commentContainer.slideUp('slow', function() {
$(this).remove();
});
$('#load').fadeOut();
}
}
});
关于php - 简单的 jquery ajax 错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1257802/