NiceTry
NiceTry

Reputation: 360

cannot display the data got via ajax call

Just want to display following information which I got from my controller via ajax call:

{"results":{"name":"Bob","city":"Ottawa","reg_date":"12-Feb-2004"}}

Here is my controller:

$data["results"] = $this->my_model->did_get_data($user_id);
                echo json_encode($data);

Here is My view:

<div id="display"></div>

Here is my JS file:

$.get('controller/get_data', function (data) {

            $( "#display").text(data.results); //???

    }, "json");

Upvotes: 1

Views: 49

Answers (3)

wallop
wallop

Reputation: 2581

Your scenario is not well explained. What is the error u get? And have u checked if $("#display") exists?

However there is one thing you could do:

$.get('controller/get_data', function (data) {

            $( "#display").text(JSON.stringify(data.results)); //???

    }, "json");

The above is if you want to print for checking purpose however if your #display should have only the values of your data i.e. name and such then you need to use the above answer i.e.

var name = data.results.name;

$("#display").html(name);

Upvotes: 1

Khalid
Khalid

Reputation: 4808

If you need to display all the attributes inside data then you can do it like this:

$.get('controller/get_data', function (data) {

   var name = data.results.name;
   var city = data.results.city;
   var reg_date = data.results.reg_date;

            $("#display").text("name :" + name + ", city:" + city + ", reg date:" + reg_date);

    }, "json");

Upvotes: 3

Brennan
Brennan

Reputation: 5742

If you want to display those attributes, you need to specify them individually:

var string = "Name: "+data.results.name+", City:"+data.results.city;
$( "#display").text(string);

Or something along those lines.

Upvotes: 1

Related Questions