0

I am new in Ajax. I have done a Ajax call and Json is coming like:

   [{"id":"4","item_name":"Chalk","unit_price":"2"}]  

Now i want to print item_name and unit_price in row so i written following code:

   jQuery(document).ready(function() {
var cnt = 1;
jQuery('#addItem').on('click', function() {
    jQuery("#items").show();

    var item_id = jQuery('#classlist1').val();
    jQuery.ajax({

        type: 'GET',
        url: 'index.php?option=com_notifint&task=getIteminfo&format=raw&tmpl=component&item_id='+item_id,


        success: function(data) {
        console.log(data)
             alert(data);
              var json_x = jQuery.parseJSON(JSON.stringify(data));

            var iname = json_x['item_name']
            alert(iname);

            jQuery("#items tbody:first").append('<tr class="item-row"><td>' + cnt + '</td><td>' + iname + '</td><td><class="cost">' + uprice + '</td><td><input class="qty" value="0" /></td><td><span class="price">0</span></td><td><div class="delete-wpr"><a class="delete" href="javascript:;" title="Remove row">X</a></div></td></tr>');

            cnt++;

        }

    });
});

});

but it takes iname value blank so please tell me solution. Taks in advance.

2 Answers2

2

json_x is an array, you need to index it:

var iname = json_x[0].item_name;

Also, don't call JSON.stringify(data), since the response is already in JSON format. It should be:

json_x = jQuery.parseJSON(data);
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Maybe you should try:

var json_x = JSON.Parse(data);

Dieter Pollier
  • 689
  • 1
  • 5
  • 11