Issue
This is my AJAX error function:
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
//var test = $.parseJSON(jqXHR.responseText);
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
Here in this jqXHR.responseText
gives me response like
{"status":{"statusCode":555,"title":"Please provide the recipient","class":"requestController"}}
From this suppose I want to display only the title from this response in alert box how can I do this?
I tried with jqXHR.status
, but it's giving me the status code. Also jqXHR.title
is giving me undefined
value.
So please anybody tell me how can I extract this particular field from the response.
Solution
Your jqXHR.responseText
is JSON, so you want to use JSON.parse
to convert it into a JavaScript object literal access the title
:
var respJson = JSON.parse( jqXHR.responseText );
console.log( respJson.status.title ); // will print out "Please provide the recipient"
Answered By - go-oleg Answer Checked By - Pedro (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.