PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] How to get the response title from the "jqXHR" object using jQuery?

 September 08, 2022     ajax, javascript, jquery     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing