tags:

views:

413

answers:

3

Hello all.I have a simple jquery/ajax request to the server which returns the structure and data of an array. I was wondering if there was a quick way in which I can use this array structure and data using jquery;

A simple request;

var token = $("#token").val();
$.ajax({ 
    type: 'POST', url: './', data: 'token=' + token + '&re=8', cache: false, timeout: 5000,
    success: function(html){ 
        // do something here with the html var 
    }                           
}); 

the result ( actual result from PHP's print_r(); );

    Array
    (

        [0] => Array
            (
                [username] => Emmalene
                [contents] => 
                              <ul><li class="name">ACTwebDesigns</li><li class="speech">helllllllo</li></ul>
                              <ul><li class="name">ACTwebDesigns</li><li class="speech">sds</li></ul>
                              <ul><li class="name">ACTwebDesigns</li><li class="speech">Sponge</li><li class="speech">dick</li></ul>
                              <ul><li class="name">ACTwebDesigns</li><li class="speech">arghh</li></ul>
            )

    )

I was thinking along the lines of

var demo = Array(html); // and then do something with the demo var

Not sure if that would work it just sprang to mind.

Any help is much appreciated.

+2  A: 

Use JSON. JSON is a lightweight data-interchange format which makes it easy to transfer data between different programming languages.

Use json_encode in PHP to encode your data:

echo json_encode($array);

And in jQuery, define that the result is in JSON format and jQuery will automatically parse it as such:

$.ajax({ 
    type: 'POST',
    url: './', data: 'token=' + token + '&re=8',
    cache: false,
    timeout: 5000,
    dataType: 'json',
    success: function(obj) { 
        // obj is now the same array as JS object:
        $.each(obj, function(index, row) {
            alert(row.username);
        });            
    }                           
}); 
Tatu Ulmanen
sorry I can only vote one for the answer but thanks all. One other thing ( i really am bad a javascript ) how do I loop through the results?
Phil Jackson
@Phil Jackson, I modified my answer to include an example of looping through the results using jQuery's `each()` method.
Tatu Ulmanen
you a good man!
Phil Jackson
still not working...
Phil Jackson
returning lots of undefineds
Phil Jackson
@Phil Jackson, you can also try `for()` loops, there's plenty of coverage on that subject even here on SO, so I won't go in deeper detail in that. Post a new question if you have problems! :)
Tatu Ulmanen
+1  A: 

Use json_encode(). It turns the array into JSON data that is directly usable in Javascript.

Pekka
+1  A: 

You could use json_encode on your PHP script. This will return a JSON encoded data that you can directly use in javascript:

$.ajax({ 
    type: 'POST', 
    url: './', 
    data: { token: token, re: '8' }, 
    cache: false, 
    timeout: 5000,
    success: function(data){ 
        // data will already be a javascript object that you can manipulate
    }                           
}); 
Darin Dimitrov
`data` will not be an javascript object unless you specify `dataType: 'json'`.
Tatu Ulmanen
It will be if the server sends the correct content type: `Content-Type: application/json`.
Darin Dimitrov
I have not writen a content type nor changed type to 'json' but still returns correctly alert(JSON_array[0].username); returns emmalene
Phil Jackson