6

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.

3 Answers 3

5

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);
        });            
    }                           
}); 
Sign up to request clarification or add additional context in comments.

3 Comments

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, I modified my answer to include an example of looping through the results using jQuery's each() method.
@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! :)
1

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

Comments

1

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
    }                           
}); 

3 Comments

data will not be an javascript object unless you specify dataType: 'json'.
It will be if the server sends the correct content type: Content-Type: application/json.
I have not writen a content type nor changed type to 'json' but still returns correctly alert(JSON_array[0].username); returns emmalene

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.