DUMB_CODER
DUMB_CODER

Reputation: 81

Fetch array values using foreach from ajax response

Hi guys this is result array from ajax response

[{"qty":1,"name":"7-test-Professional","part_number":"12231","list_price":"800"},
{"qty":1,"name":"Senior Professional Forester","part_number":"","list_price":"97.000000"]

where i am trying to alert each value as

$.ajax({
    type: 'POST',
    url: 'getdata.php?product_id='+rate_id,
    success: function(data)
    {    
        // console.log(result);
        result=$.parseJSON( data );
        $.each(result, function( index, value ) {
            alert( index + ": " + value );
        });
        //alert(result); //this prints the above array !!               
    }
});

getting output as : index_value: [object Object]

Thanks for your time.. :)

Upvotes: 1

Views: 5724

Answers (1)

Abu Talha Danish
Abu Talha Danish

Reputation: 291

Try adding dataType: 'json' into your ajax parameters
You wouldn't need parseJSON I hope.

The final code becomes

       $.ajax(
          {
            type: 'POST',
            url: 'getdata.php?product_id='+rate_id,
            dataType: 'json',
            success: function(data)
            {    
              $.each(result, function( index, value ) {
                   alert( index + ": " + value );
              });           
            }
        });

Upvotes: 1

Related Questions