How to trigger jquery ajax error callback based on server response not HTTP 500

0 votes

By using jquery ajax function, I can do something like:

$.ajax({

  url: url,
  type: 'GET',
  async: true,
  dataType: 'json',
  data: data,

 success: function(data) {

     //Handle server response here

  },

 error: function(xhr, status, error){

    //Handle failure here

 }

});

I'd like to ask if server specifically returns to me a JSON object with string value like {message: 'There is an error'}, could server do something so that this response could be handled in jquery.ajax() error callback instead of success callback?

Jun 18, 2020 in Java-Script by kartik
• 37,510 points
16,343 views

1 answer to this question.

0 votes

Hello @kartik,

The error callback will be executed when the response from the server is not going to be what you were expecting. So for example in this situations it:

  • HTTP 404/500 or any other HTTP error message has been received
  • data of incorrect type was received (i.e. you have expected JSON, you have received something else).

In your situation the data is correct (it's a JSON message). If you want to manually trigger the error callback based on the value of the received data you can do so quite simple. Just change the anonymous callback for error to named function.

function handleError(xhr, status, error){

    //Handle failure here

 }

$.ajax({

  url: url,
  type: 'GET',
  async: true,
  dataType: 'json',
  data: data,

 success: function(data) {
     if (whatever) {
         handleError(xhr, status, ''); // manually trigger callback
     }
     //Handle server response here

  },

 error: handleError
});

Hope it helps!!

answered Jun 18, 2020 by Niroj
• 82,880 points

Related Questions In Java-Script

0 votes
3 answers
0 votes
1 answer

How to Abort Ajax requests using jQuery?

Hello kartik, Just use ajax.abort(). For example you could ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
8,762 views
0 votes
1 answer

How to manage a redirect request after a jQuery Ajax call?

Hii kartik, You can resolved this issue like ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
28,265 views
+1 vote
1 answer

How to get error status code of http get NodeJS?

Hello @kartik, An error code 400 response is ...READ MORE

answered Jun 2, 2020 in Java-Script by Niroj
• 82,880 points
3,879 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
21,926 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,880 points
2,697 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
2,567 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,880 points
41,942 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,880 points
3,722 views
0 votes
1 answer

How to get the jQuery $.ajax error response text?

Hello @kartik, Try: error: function(xhr, status, error) { ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,880 points
29,365 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP