Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. ==with response codes==
  2. function errorHandler( xhr, status, data ) {
  3. if( status === 500 ) {
  4. reportMessage( 'There was problem with server' );
  5. } else if( status === 200 ) {
  6. if( typeof data === 'string' ) {
  7. data = JSON.stringify( data );
  8. }
  9. // inspect the data and work out the problem
  10. }
  11. }
  12.  
  13. $.ajax({
  14. url: '/api.php...',
  15. success: function( data, status, xhr ) {
  16. if( data.error ) {
  17. errorHandler( xhr, status, data );
  18. } else {
  19. // success handler
  20. }
  21. },
  22. error: errorHandler
  23. })
  24.  
  25. ==without response codes==
  26. function errorHandler( xhr, status, data ) {
  27. data = JSON.stringify( data );
  28. if( status === 500 ) {
  29. reportMessage( 'There was problem with server' );
  30. } else if( status === 404 ) {
  31. reportMessage( 'response not found' );
  32. } else if( status === 400 ) {
  33. reportMessage( 'problem with parameters' );
  34. // dive into data to see what problem is
  35. }
  36. }
  37.  
  38. $.ajax({
  39. url: '/api.php...',
  40. success: function( data, status, xhr ) {
  41. // success handler
  42. },
  43. error: errorHandler
  44. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement