0

I have used json_encode to encode two php arrays and now i have to read through ajax. Could anyone please let me know how to read those arrays through ajax request.

For example: i have a php file file1.php which has

echo json_encode($array1);
echo json_encode($array2);

Another file in which i read as follows:

For reading single encoded array i am reading like this

new Ajax.Request("file1.php",
       {
         method:'get',
         asynchronous:false,
         parameters: ({id: stopID, contains: tempContain}),
          onSuccess:function(data){
                var result=data.responseJSON;

                var keys = Object.keys(result);
                var values = Object.values(result);

                for(var i = 0; i < keys.length; i++) {
                     infoString += keys[i]+":"+values[i];
                }               
  });
2
  • 1
    You'll have to post the contents of your php arrays. Also, are you doing this through vanilla JavaScript or are you using a library like jQuery? Commented Mar 22, 2011 at 15:14
  • 1
    actually, can you show us what you have so far? did you successfully get the ajax to return the encoded json? Commented Mar 22, 2011 at 15:18

2 Answers 2

1

You can use jquery, it will save you a lot of time ;) There are examples in this link:

http://api.jquery.com/jQuery.getJSON/

Sign up to request clarification or add additional context in comments.

Comments

0

With jQuery Ajax

$.ajax({
  url: '/path/to/file',
  type: 'POST',
  dataType: 'json',
  data: {param1: 'value1'},
  complete: function(xhr, textStatus) {
    //called when complete
  },
  success: function(data, textStatus, xhr) {
    //called when successful
  },
  error: function(xhr, textStatus, errorThrown) {
    //called when there is an error
  }
});


Comments

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.