使$ .ajax失败

This is rather an odd question, but, I want to make $.ajax fail to check my fail trigger in it, I have this jQuery code:

$(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...");
            }
        });
    });
});

And am calling this PHP code:

<?php
function foo()
{
    return "bar";
}

print foo();

return false;
?>

and want to make sure that the fail: ... is working. How do I do this?

EDIT

I have amended the text.php to be this:

<?php
throw new LogicException("Failed");

Also:

<?php
header("HTTP://this.does.not.exist");

But this still shows in the $(".log") area even though it has failed.

Noticed I was using the fail function wrong, now amended my jQuery to:

$(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!");
        });
    });
});

Also if it helps, I am using these scripts for jQuery libraries:

<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>

You could send a header returning an error.

For example:

header("HTTP/1.0 404 Not Found");

Or of course have your script generate a fatal error ;-)

Two suggestions:

  1. Change the .fail callback call to .error and

  2. Try it by returning an error status code from your server like jeroen suggested,

    header("not-real.php", true, 404);