这是一个相当奇怪的问题,但是,我想让 $.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...");
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
我正在调用这个 PHP 代码:
<?php
function foo()
{
return "bar";
}
print foo();
return false;
?>
Run Code Online (Sandbox Code Playgroud)
并希望确保fail: ...正在工作。我该怎么做呢?
编辑
我已将 text.php 修改为:
<?php
throw new LogicException("Failed");
Run Code Online (Sandbox Code Playgroud)
还:
<?php
header("HTTP://this.does.not.exist");
Run Code Online (Sandbox Code Playgroud)
但这仍然显示在该$(".log")区域,即使它失败了。
注意到我错误地使用了失败函数,现在将我的 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!");
});
});
});
Run Code Online (Sandbox Code Playgroud)
另外,如果有帮助,我将这些脚本用于 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>
Run Code Online (Sandbox Code Playgroud)
两个建议:
将回调调用更改.fail为.error和
像 jeroen 建议的那样,通过从服务器返回错误状态代码来尝试,
header("not-real.php", true, 404);
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
1313 次 |
| 最近记录: |