Bruno
Bruno

Reputation: 6449

How to convert json to plain text?

How to convert JSON to plain text?

{"Days":["is not a number"]} to Days is not a number.

Here is the code:

$('.best_in_place').bind("ajax:error", function(jqXHR,error, errorThrown) {
  alert(error.responseText);
});

Upvotes: 0

Views: 10145

Answers (3)

Emmadi Sumith Kumar
Emmadi Sumith Kumar

Reputation: 300

Use json-to-plain-text npm module

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

Convert the response into JSON object and parse its key-value pair

var error = JSON.parse( error.responseText );
for( var name in error ) {
    console.log( name + " " + error[ name ] ); // Days is not a number
}

Upvotes: 2

insertusernamehere
insertusernamehere

Reputation: 23580

As you're using jQuery, this might help:

var result = '';
$.each(error.responseText, function(key, value) {
    result += key + ' ' + value;
});

This will also work and can easily be adjusted if the response holds multiple key-value-pairs.

Demo

Try before buy

Upvotes: 2

Related Questions