Request:

$.ajax({ type: "POST", url: req_ajx_host + "ajx/sharecount/", data: "&projid=<?=$proj['id'];?>", success: function (data) { $("#sharecount").html(data); alert(data); } }); 

Displays the string:

[{"sco": "12"}]

How to make an array from a string and output the value 12?

  • data[0]['sco'] - Vitaly Emelyantsev

2 answers 2

 $.ajax({ type: "POST", url: req_ajx_host + "ajx/sharecount/", data: "&projid=<?=$proj['id'];?>", dataType: 'json', success: function (data) { $("#sharecount").html(data[0].sco); alert(data[0].sco) } }); 

That is how it should, in theory, earn.

  • writes undefined - Artur Cooper
  • yes, added dataType and did data [0] .sco - returned 0. Thank you. - Artur Cooper

More compact option:

 $.post(req_ajx_host+'ajx/sharecount/', {projid: <?=$proj['id'];?>}, function(data) { $("#sharecount").html(data[0].sco); }, 'json');