-1

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>
Zak
  • 6,976
  • 2
  • 26
  • 48
  • Check those two links, they will help you: https://stackoverflow.com/questions/8401969/jquery-ajax-data-to-html https://stackoverflow.com/questions/9098649/jquery-ajax-request-with-json-response-how-to – tt123 Jun 20 '22 at 19:49
  • 1
    It will be in `$_POST['product_id']` in PHP. – Barmar Jun 20 '22 at 19:50
  • Why do you have `dataType: 'html'` if the response is supposed to be JSON? Use `dataType: 'json'` and jQuery will automatically call `JSON.parse()` for you. – Barmar Jun 20 '22 at 19:51
  • i get console.log(data) form inspect console , {"data":[{"product_price":"3000"}]} – kotakkecil Jun 20 '22 at 19:59

1 Answers1

0

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>
Lucretius
  • 1,053
  • 1
  • 13
  • 26