0

I have the following function, that get a response from a web service. The response should be displayed on one section of the website. That's working fine, but the thing is, in case of error, I've trying to get the error message and display it in the same way as the succesful response does, but I can't get that.

$(document).ready(function(){
$('.button').click(function(){
    try {
        var $resp = $.get("service url here", function(resp){
        $('.response').append(resp.response.greeting + ", " + resp.response.Welcome);
        });
    }
    catch (err){
        $('.response').append(err.name + ", "+ err.message);
    }

    });
});
Gonzalo
  • 13
  • 1
  • 2
    Possible duplicate of [jQuery Ajax error handling, show custom exception messages](http://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages) – Michał Perłakowski Jan 26 '16 at 02:29

1 Answers1

0

Try using .always()

$(document).ready(function(){
  $(".button").click(function() {
    $.get("service url here")
    .always(function(resp, textStatus, jqxhr) {
        $(".response")
        .append(textStatus === "success" 
          ? resp.response.greeting + ", " + resp.response.Welcome
          : textStatus + ", "+ jqxhr
        );    
    });
});
guest271314
  • 1
  • 15
  • 104
  • 177