Jquery Ajax after function success / error

I have a post function that misses the error.

Here the value is 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 work, but only the success function. It will display the message “No group selection”.

Thanks for any help!

+6
source share
3 answers

The error parameter in jQuery ajax methods refers to errors caused by a bad connection, timeout, wrong URL, such things. This is not the mistake you are thinking about.

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; }); 
+21
source

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

Instead of changing the HTTP header, I find it useful to return information in a json response. So, for example, I would answer postride.php like this:

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

Then in a “successful” JS callback, I would do the following:

 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 } 
+2
source

The error method of the jQuery ajax function is called only when the requested page returns an error status code. Your example will probably return HTTP 200, so success is called; jQuery does not query the content of the response to determine if it was successful.

You must set the status code to 4xx if $group_id is 0, replacing your echo call with a header call.

0
source

Source: https://habr.com/ru/post/897440/


All Articles