Make a mistake $ .ajax

This is a rather strange question, but I want $ .ajax not to be able to check my crash 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 I am calling this PHP code:

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

print foo();

return false;
?>

      

and make sure it works fail: ...

. How to do it?

EDIT

I changed text.php as follows:

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

      

also:

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

      

But this is still displayed in the area $(".log")

even if it is not done.

Noticed that I was using the crash function incorrectly, now changed 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 use these scripts for the 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>

      

+3


source to share


2 answers


Two suggestions:



  • Change the callback .fail

    to .error

    and

  • Try it by returning the error status code from your server as jeroen suggested,

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

+2


source


You can send a header that returns an error.

For example:



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

      

Or, of course, your script generates a fatal error; -)

+3


source







All Articles