我通过AJAX运行以下php脚本,需要将一些错误管理集成到其中:
$exists = file_exists('../temp/' . $email . '/index.html');
if($exists) {
echo "ERROR!!!";
} else {
createUserDirectory($email);
}
在AJAX成功函数中,如何确定脚本是成功运行还是产生错误?
如果它返回OK,我想暂时执行重定向,但如果出现错误,我想将错误添加到文档中的DIV(而不是重定向当然...)。
$.ajax({
type: "POST",
url: 'generate/build.php',
data: $("#generateStart").serialize(), // serializes the form's elements.
success: function(data)
{
window.location.href="generate-site.php?user=" + data.replace(/^ +/,"") + ""; // Redirect to publish.php and remove any spaces from the URL (bug fix).
}
});
感谢。
答案 0 :(得分:4)
您的PHP脚本应返回4xx
或5xx
HTTP status code以指示失败。然后,将调用jQuery's ajax object的error
方法。
答案 1 :(得分:0)
在success
处理程序中,选中if(data == 'ERROR!!!')
。
答案 2 :(得分:0)
您可能希望为此添加两个部分:$ .ajax函数上的错误回调,以查看网络上的请求是否失败,然后检查返回值以查看服务器验证是否失败(如果是文件)在这种情况下存在)。
示例:
$.ajax({
...
success : function(data) {
if(data && data != "ERROR!!!") {
//redirect
}
},
error: function(jqXHR, textStatus, errorThrown) {
//Log error, display feedback to user, etc...
}
);