Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Ajax post success/error function

Tags:

jquery

I have a post function that is not being caught on error.

Here's the value being returned from postride.php.

if ($group_id==0) {
echo 'No group selected';
return false;
exit;
}

Here's the jquery code:

$(document).ready(function(){
$('#postride').submit(function(event) {
     event.preventDefault();
     dataString = $("#postride").serialize();
     $.ajax({
        type: "post",
        url: "postride.php",
        data:dataString,
        error: function(returnval) {
            $(".message").text(returnval + " failure");
            $(".message").fadeIn("slow");
            $(".message").delay(2000).fadeOut(1000);
        },
        success: function (returnval) {
            $(".message").text(returnval + " success");
            $(".message").fadeIn("slow");
            $(".message").delay(2000).fadeOut(1000);    
            //setTimeout( function() { top.location.href="view.php" }, 3000 );  
        }
    })
    return false;
});

});

The post function returns false, but the error function does not fire, only the success function. It will post "No group selected success".

Thanks for any help!

like image 565
Matt Avatar asked Sep 16 '11 02:09

Matt


2 Answers

wsanville is correct: "success" or "failure" refers to the success/failure of the ajax request.

Rather than changing up the HTTP header, though, I find it useful to return information in a json response. So for instance, I would have postride.php respond like so:

$error = false;
if ($group_id==0) {
    $error = 'No group selected';
}
$response = array('error'=>$error);
echo json_encode($response);

Then in the "success" callback of the JS, I would do this:

success: function (returnval) {
    if (returnval.error) {
      $(".message").text(returnval + " failure")
        .fadeIn("slow",function(){
            $(this).delay(2000).fadeOut(1000);
        });
    } else {
      //do whatever user should see for succcess
    }
like image 177
Philip Schweiger Avatar answered Oct 18 '22 02:10

Philip Schweiger


The error option in jQuery ajax methods is for errors caused by a bad connection, timeout, invalid url, things of that nature. It's not the kind of error that you're thinking.

What most people do is something like this...

php

if ($group_id == 0) {
echo json_encode(array(
    'status' => 'error',
    'message'=> 'error message'
));
}
else
{
echo json_encode(array(
    'status' => 'success',
    'message'=> 'success message'
));
}

javascript

$(document).ready(function(){
$('#postride').submit(function(event) {
     event.preventDefault();
     dataString = $("#postride").serialize();
     $.ajax({
        type: "post",
        url: "postride.php",
        dataType:"json",
        data: dataString,
        success: function (response) {
            if(response.status === "success") {
                // do something with response.message or whatever other data on success
            } else if(response.status === "error") {
                // do something with response.message or whatever other data on error
            }
        }
    })
    return false;
});
like image 36
Anthony Jack Avatar answered Oct 18 '22 03:10

Anthony Jack