Home > Mobile >  how get data form json from ajax ? to javascript
how get data form json from ajax ? to javascript

Time:06-21

with ajax json data : {"data":[{"product_price":"3000"}]}

how call data :3000 from data and product price

$.ajax({
            type: "POST",
            dataType: 'html',
            url: '<?php echo base_url() . "index.php/product/getdataJson/" ?>'   product_id,
            data: {
                'product_id': product_id
            },
            success: function(data) {

                var data_json = JSON.parse(data);  // dont work

                document.getElementById("demo").innerHTML = data_json.data ; // dont work

      },
            error: function() {
                alert("did not work");
            }

        });





<p id="demo"></p>

CodePudding user response:

You need to drill down into the json object to get the value you're after.

var data = '{"data":[{"product_price":"3000"}]}'

var data_json = JSON.parse( data );
console.log( data_json.data );
console.log( data_json.data[0].product_price );
document.getElementById("demo").innerHTML = data_json.data[0].product_price;
<p id="demo"></p>

  • Related