-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>
4
  • Check those two links, they will help you: stackoverflow.com/questions/8401969/jquery-ajax-data-to-html stackoverflow.com/questions/9098649/… Commented Jun 20, 2022 at 19:49
  • 1
    It will be in $_POST['product_id'] in PHP. Commented Jun 20, 2022 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. Commented Jun 20, 2022 at 19:51
  • i get console.log(data) form inspect console , {"data":[{"product_price":"3000"}]} Commented Jun 20, 2022 at 19:59

1 Answer 1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.