code_junkie

How do you handle errors from AJAX calls?

Nov 13th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. $.ajax({
  2. type: "POST",
  3. url: "MyUrl",
  4. data: "val1=test",
  5. success: function(result){
  6. // Do stuff
  7. }
  8. });
  9.  
  10. $.ajax({
  11. type: "POST",
  12. url: "MyUrl",
  13. data: "val1=test",
  14. success: function(result){
  15. if (result.error == "true")
  16. {
  17. alert("An error occurred: " & result.errorMessage);
  18. }
  19. else
  20. {
  21. // Do stuff
  22. }
  23. }
  24. });
  25.  
  26. $.ajaxSetup({
  27. error: function(xhr){
  28. alert('Request Status: ' + xhr.status + ' Status Text: ' + xhr.statusText + ' ' + xhr.responseText);
  29. }
  30. });
  31.  
  32. header('HTTP/1.0 419 Custom Error');
  33.  
  34. $.ajax({
  35. type: "POST",
  36. url: "MyUrl",
  37. data: "val1=test",
  38. success: function(result){
  39. // Do stuff
  40. },
  41. error: function(request,status,errorThrown) {
  42. // There's been an error, do something with it!
  43. // Only use status and errorThrown.
  44. // Chances are request will not have anything in it.
  45. }
  46. });
Add Comment
Please, Sign In to add comment