我正在通过 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).
}
});
谢谢。
最佳答案
您的 PHP 脚本应返回 4xx
或 5xx
HTTP status code来指示失败。然后,jQuery's ajax object的error
方法将被调用。
关于php - jQuery ajax 成功条件..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9469904/