top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Exception handling in Jquery

+1 vote
44 views

I am making an Ajax call using Jquery. Sometimes I get error/exception from the back-end. I want to catch them show appropriate error message to the user.

How can I handle exception/error scenarios with Jquery?

posted Mar 25, 2013 by Sudheendra

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+1 vote
 
Best answer

The code looks a little something like this:

$.get('url', function() {
    alert("success");
})
.error(function() {
    alert("error");
})

Another snazzy way of dealing with error handling

$.get("url")
.done(function(){ 
    alert("success");
})
.fail(function(){
    alert("error(fail)");
});

You could also use the .ajaxError() method, which involves too much prep work for my taste.

.ajaxError() example:

$(document).ajaxError(function(e, xhr, settings, exception) { 
    alert("error"); 
});

I find that the $.get().error() chaining method is more efficient as it involves the least amount of lines of code. And when you’re working with the mobile web, every line counts.

answer Mar 25, 2013 by Nora Jones
Similar Questions
+3 votes

The browser i am using is google chrome. I also tested it in internet explorer and firefox too. But result is same. The post method is working fine for the same rest endpoint, but when i am trying to execute delete method on the same endpoint, it's throwing error with http status code 209 with the reason "failed to form full key for endpoint".

This is how the i am sending the delete request.

$.ajax({
             url: url,
             type: "DELETE",
             dataType:"json",
             data: JSON.stringify(data),
             crossDomain: true,
             contentType:"application/json",
             success:function(result){console.log("Success " + console.log(JSON.stringify(result)))},
             error:function(error){
                        alert("Error Deleting Service.See the console log.");
                        console.log(JSON.stringify(error));
                        },
             }).then(function(response){
                   console.log(JSON.stringify(response));
             });

The POST method:

 $.ajax({
                 url: url,
                 type: "POST",
                 dataType:"json",
                 data: JSON.stringify(data),
                 crossDomain: true,
                 contentType:"application/json; charset=utf-8",
                 success:function(){console.log("Success")},
                 error:function(error){console.log(JSON.stringify(error))},
                 }).then(function(response){
                      console.log(JSON.stringify(response));
                 });

Need help. I have already spent the day on this but not able to find the solution for this. I also tried to append the data params with the url but that too didn't worked.


Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.
QUERY HOME
...