Prasath Govind
Prasath Govind

Reputation: 750

Unable to trigger error callback in jQuery AJAX using PHP

I am trying to test the error callback using the below scenarios :

  1. Change my url : "telemetry.php" to url : "asdasfjgafas.php"
  2. Have <?php header("HTTP/1.1 404 Not Found"); exit() > inside telemetry.php

But, I don't get alert() of the error callback even after trying 1. and 2. separately!

My .js content making AJAX request using jQuery :

$.ajax({

    url     :   "telemetry.php",
    data    :   ({
                    DshBrdName  : strFullDashBoardName,
                    DshBrdID    : currentDashboard, 
                    r           : Math.random()
                }),
    success :   function(data, textStatus, jQxhr){
                    alert(textStatus); 
                },
    error   :   function(jqXHR, textStatus, errorThrown){
                    alert(textStatus);
                    alert(errorThrown);
                },
    type    :   "POST"
});

P.S : I am able to get success alert when I have valid url and valid php code!

Upvotes: 0

Views: 57

Answers (1)

kennasoft
kennasoft

Reputation: 1593

I can see a typo in your error block:

function(jqXHR, textStatus, errorThrown){
                alert(testStatus); // should be textStatus?
                alert(errorThrown);
            },

EDIT

From the js, point of view, everything seems ok, as your snippet below (I replaced the undefined variables with string) alerts the error (since this php script obviously isn't found). You may want to give more detail about what goes on at the server side...

$.ajax({

    url     :   "telemetry.php",
    data    :   ({
                    DshBrdName  : 'strFullDashBoardName',
                    DshBrdID    : 'currentDashboard', 
                    r           : Math.random()
                }),
    success :   function(data, textStatus, jQxhr){
                    alert(textStatus); 
                },
    error   :   function(jqXHR, textStatus, errorThrown){
                    alert(textStatus);
                    alert(errorThrown);
                },
    type    :   "POST"
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 1

Related Questions