这是一个有些奇怪的问题,但我想让$.ajax失败以检查其中的失败触发器,我有这个jQuery代码:
$(document).ready(function () {
$("#trigger").on("click", function () {
$.ajax({
url: "text.php",
success: function (data) {
$(".log").html(data.slice(0, 1000));
//alert("Load has been performed");
},
fail: function () {
$(".msg").html("Something went wrong...");
}
});
});
});
我正在调用这段PHP代码:
<?php
function foo()
{
return "bar";
}
print foo();
return false;
?>
我想确保
fail: ...
起作用了,我该怎么做呢?
编辑
我已将text.php修改为以下内容:<?php
throw new LogicException("Failed");
此外:
<?php
header("HTTP://this.does.not.exist");
即使它失败了,但这仍然会显示在$(".log")
区域。
注意到我使用了错误的fail函数,现在我已经更正了我的jQuery:
$(document).ready(function () {
$("#trigger").on("click", function () {
$.ajax({
url: "text.php"
}).done(function (data) {
$(".log").html(data.slice(0, 1000));
}).fail(function () {
$(".msg").html("Something went wrong...");
}).always(function () {
alert("One way or another, we did it!");
});
});
});
如果有帮助的话,我正在使用这些jQuery库的脚本:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>