Jump to content

Help with JSON in JQuery

vibeit

Hello, I need help parsing a json string.

 

Inside the getdata.php file:

<?php

$val1 = "break";
$val2 = "break2";

echo json_encode(array("name"=>$val1, "address" => $val2));

?>

 

Inside my data.js file:

//1st method

$('#btndata').on('click', function(){
    
    $.post('getdata.php', function(e){
        var obj = jQuery.parseJSON(e);  <------- nothing happens when I click the button.
        $('#lbldata').html(obj.name);
    });
    
});

 

But if I type it this way:

//Second method

$('#btndata').on('click', function(){
    
    $.post('getdata.php', function(e){
        $('#lbldata').html(e); <----------- output in browser: {"name":"break","address":"break2"}
    });

 

    
});

 

What's puzzling here is when I used the first method in another project, it works. Can anyone help me sort this out? I even tried ajax and still got nothing.

 

Thanks

My system: CPU: Intel i5 6500; Mobo: H110M-k; GPU: Nvidia GT 730; Memory: 16 GB; HDD: 2x 1TB HDD;

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×