Welcome Guest, Not a member yet? Register   Sign In
Ajax Post with jQuery and Json
#1

[eluser]Fireclave[/eluser]
Hey Community,

i try to get Datas with jQuery and Codeigniter (2.0.1) - the Query works.

My Problem is:

I using

JS

Code:
$.post("<?php echo base_url() ?>welcome/get_ajax_course2", { id: 10 }, function(data) {
            alert("Data Loaded: " + data );
            
            alert(data.1); // This is not working
            alert(data.name);
            
            //$('#wysiwyg_titel').val();
        }, "json");

PHP

Code:
echo json_encode($this->db->query()->result_array());

to get the the result. My Problem is: How can i get the values ?

I have tried another examples with Json. Can i Take the Values like:
$data[0]['name'] in Javascript ?

My PHP-Array is like this:

Code:
$data = array(
    array(...),
    array(...) );

EDIT:

When i use alert(data) i get the following:

Code:
[{"id":"1","actual_role_id":"0","name":"Guest","description":"This is the default role everyone is if they're not logged in. Site visitors are guests until they log in."},{"id":"2","actual_role_id":"1","name":"User","description":"Standard registered user."},{"id":"3","actual_role_id":"2","name":"Client","description":"Client priveleges such as submitting things and editing their own stuff."}]

this is only an example database ;-)

How can i get the description of the second Row ? (data.row2.description ??)
#2

[eluser]dannyded[/eluser]
you have to parse data response.. try it this way:

Code:
$.post("<?php echo base_url() ?>welcome/get_ajax_course2", { id: 10 }, function(data) {
            alert("Data Loaded: " + data );

            // parse response
            data = jQuery.parseJSON(data );

            alert(data.1); // This is not working
            alert(data.name);
            
            //$('#wysiwyg_titel').val();
        }, "json");

[code]
#3

[eluser]Fireclave[/eluser]
data = jQuery.parseJSON(data );

and then ? how can i call the rows/cells ?
#4

[eluser]dannyded[/eluser]
if this is your raw response:
Code:
[{"id":"1","actual_role_id":"0","name":"Guest","description":"This is the default role everyone is if they're not logged in. Site visitors are guests until they log in."},{"id":"2","actual_role_id":"1","name":"User","description":"Standard registered user."},{"id":"3","actual_role_id":"2","name":"Client","description":"Client priveleges such as submitting things and editing their own stuff."}]


after you parse data like I told you:

Code:
// parse response
            data = jQuery.parseJSON(data );

then you can access your data like this:

Code:
alert (data.id)
           alert (data.name)
           alert (data.description)
           // etc ...




Theme © iAndrew 2016 - Forum software by © MyBB