Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON data print in (element).html in HTML

What I am actually trying to achieve, I am doing query in codeigniter. Data is coming properly but, I am quite unable to show it to HTML element. Please check json output in firebug console and code as below-

JSON Output:

{"founduser":
    {
      "user_id":"2",
      "name":"Altab Hossen",
      "email":"[email protected]",
      "password":"",
      "employee_id":"0122",
      "nric_number":"K90031G",
      "is_user_active":"1"}
}

Ajax Code

var userid = $("#userid").val();                    
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: base_url+"checkuser/"+userid,
                    data: "{}",
                    dataType: "json",
                    success: function (data) {
                        alert(data);
                    },
                    error: function (result) {
                        alert("Error");
                    }
                });

Now: alert(data); returning me Object Object. I want to do show only name (Altab Hossen) in div element: $('.showusername').html(data.name);

How Can I achieve?

like image 299
Khan Avatar asked Mar 31 '26 17:03

Khan


1 Answers

Try,

$('.showusername').html(data.founduser.name);

and if you want to display a json as a string while debugging, you need to use JSON.stringify() like,

   alert(JSON.stringify(data));
like image 81
Rajaprabhu Aravindasamy Avatar answered Apr 03 '26 08:04

Rajaprabhu Aravindasamy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!