PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] how get data form json from ajax ? to javascript

 September 08, 2022     ajax, javascript     No comments   

Issue

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>

Solution

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>



Answered By - Lucretius
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing