0

I was searched any solutions but still not help. That problem is about read character. How to read characters which come from ajax respond? this is the output from ajax, as JSON object:

 ["label" : "Item1", "value" : "TOP & HEAD"] 

ideal output should be like this:

 ["label" : "Item1", "value" : "TOP & HEAD"]

this is the success function :

success: function(data){
   // what code should be added so it can be read the character properly? 
   console.log(data);
   response(data);
}

Need advice for it please..

  • what is getting in response like what is in console after ajax req success with console.log(data); – Zeeshan Apr 22 '15 at 09:24
  • what do you mean by **problem is about read character**. Which character you are accessing? where is your code to access the character? – jogesh_pi Apr 22 '15 at 09:25
  • jogesh_pi maybe exactly problem is convert "&" to "&". Above is the console.log what i get from ajax. CMIIW guys... still learn.. – user2392149 Apr 22 '15 at 09:41
  • this is the console.log(data): [ 0: object label: "item" value:"TOP & HEAD", 1:Object label:... value:... ] – user2392149 Apr 22 '15 at 09:54

4 Answers4

0

Try this,

success: function(data){
   // What code should be added so it can be read the character properly? 
   console.log(data);
   alert(data['label']);
   alert(data['value']);
}

You can access by calling the key like,

console.log(data['label']);
Zeeshan
  • 1,659
  • 13
  • 17
Abhishek Pachal
  • 554
  • 6
  • 27
0

If you have access to the web service, you could change it so it doesn't encode the text.

Otherwise, you could do what is suggested in this answer:

var decoded = $('<textarea />').html(data.value).text();
Community
  • 1
  • 1
Knelis
  • 6,782
  • 2
  • 34
  • 54
0

Take a look at this post: Decode &amp; back to & in JavaScript

basically it says you have two options (before parsing your json):

1.) You want to replace the amp only:

var decoded = data.replace(/&amp;/g, '&');

2.) You want to decode HTML characters:

var div = document.createElement('div');
div.innerHTML = data;
var decoded = div.firstChild.nodeValue;
Community
  • 1
  • 1
Chris K.
  • 1,060
  • 8
  • 10
0
function decodeEntities(html){
    var str; 
    var temp= $("<p>");
    $(temp).html(html);
    str= $(temp).text()
    $(temp)=null;
    return str;
}

console.log(decodeEntities(data["value"]));
Aneesh R S
  • 3,807
  • 4
  • 23
  • 35