Jquery Ajax Success Error Code Example

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

Bạn đang xem: Jquery ajax success error code example

Thanks for any help!

nói qua
asked Sep 16, 2011 at 2:43

13311 gold badge22 silver badges55 bronze badges
địa chỉ cửa hàng a comment |

4 Answers 4

Sorted by: Reset to default
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
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 bởi vì is something lượt thích this...


if ($group_id == 0) echo json_encode(array( "status" => "error", "message"=> "error message"));elseecho 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") // vì chưng something with response.message or whatever other data on success else if(response.status === "error") // bởi something with response.message or whatever other data on error ) return false;);
chia sẻ
edited Jul 10, 2014 at 10:25


5,84755 gold badges4848 silver badges7272 bronze badges
answered Sep 16, 2011 at 2:58

Anthony JackAnthony Jack
5,12377 gold badges2828 silver badges4747 bronze badges
showroom a bình luận |
wsanville is correct: "success" or "failure" refers khổng lồ the success/failure of the ajax request.

Rather than changing up the HTTP header, though, I find it useful lớn 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 bởi 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
tóm tắt
answered Sep 16, 2011 at 3:00


Philip SchweigerPhilip Schweiger
2,69411 gold badge1717 silver badges2727 bronze badges
địa chỉ a phản hồi |
The error method of jQuery"s ajax function will only get called when the page requested returns an error status code. Your example will probably return HTTP 200, which is why success gets called; jQuery does not interrogate the contents of a response to lớn determine whether or not it was successful.

Xem thêm: +99 Mẫu Bình Hoa Giả Để Bàn Giá Tốt Tháng 3, 2022, Top 20+ Mẫu Hoa Để Bàn Cưới Đẹp, Đơn Giản

You should mix a 4xx status code when $group_id is 0, by replacing your echo call with a điện thoại tư vấn to header.

nói qua
answered Sep 16, 2011 at 2:49

36.8k77 gold badges7474 silver badges101101 bronze badges
showroom a bình luận |
You can also use like that. Success if server is ok(200) và fail if any server-error(500) occur.

$.ajax( url: "postride.php", type: "POST", data: dataString, datatype: "json", success: function (data) successFunction(data); , error: function (jqXHR, textStatus, errorThrown) errorFunction(); );
chia sẻ
answered Aug 13, 2021 at 15:23
Mithilesh KumarMithilesh Kumar
9211 gold badge22 silver badges1313 bronze badges
địa chỉ a comment |

Your Answer

Thanks for contributing an answer to lớn Stack Overflow!

Please be sure lớn answer the question. Provide details và share your research!

But avoid

Asking for help, clarification, or responding khổng lồ other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using email and Password

Post as a guest

e-mail Required, but never shown

Post as a guest


Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to lớn our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.
The Overflow Blog Featured on Meta
Manage web application security on shared computers and kiosks
erorr:Display comments through ajax (from DB) (laravel 5.2)
Is there an "exists" function for jQuery?
How can I upload files asynchronously with jQuery?
Adding a table row in jQuery
How vị I kiểm tra if an element is hidden in jQuery?
Setting "checked" for a checkbox with jQuery
Abort Ajax requests using jQuery
How can I know which radio button is selected via jQuery?
How vày I kiểm tra whether a checkbox is checked in jQuery?
How vị I refresh a page using JavaScript?
Scroll lớn an element with jQuery
Hot Network Questions more hot questions

Question feed
Subscribe lớn RSS
Question feed to subscribe lớn this RSS feed, copy & paste this URL into your RSS reader.

Stack Overflow
Stack Exchange Network
Site kiến thiết / hình ảnh sản phẩm © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. Rev2022.10.12.39069

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device & disclose information in accordance with our Cookie Policy.