I have the following ajax call and if it errors, I have a default error message stored in a constant which I'll use if the response doesn't contain a custom message. When I look at this, I keep thinking it could be done better. Can anyone suggest how I can do it better?
function requestService(){
$.ajax({
dataType: 'json',
type: 'POST',
url: '/myurl',
contentType: CONTENT_TYPE,
timeout: AJAX_TIMEOUT,
success: function(data, textStatus, jqXHR){
populateData(JSON.parse(jqXHR.responseText))
},
error: function(err){
var errorMessage = AJAX_ERROR;
try {
errorMessage = JSON.parse(err.responseText).messages.messages[0].description;
}
catch(error) {
//There was an problem finding an error message from the server. The default message will be used.
}
displayError(errorMessage)
},
complete: function(){
console.log("complete");
}
});
};