1

I have a javascript that does an AJAX-request to a PHP-script which starts a loop. This loop returns data to the javascript. I want to be able to send arrays from the PHP-script back to the javascript but this doesn't seem to be working properly.

Mainly because it sometimes returns 2(or more) arrays at the same time. How would I get this to work? Tried searching for JSON-help but didn't find anything that explained my problem.

In my HTTP-Response method:

if(http.readyState == 3)
{

      console.log( http.responseText );
      var toBeEvaled = "(" + http.responseText + ")";
      console.log( toBeEvaled );
      var textout = eval( toBeEvaled );

      console.log( textout.name );

}

My PHP looks like this:

echo json_encode( array( 'type' => 1, 'name' => $stringVar, 'id' => $id ) );

Log 1 becomes:

{"type":1,"name":"String1","id":"1000004"}
{"type":1,"name":"String2","id":"60220"}

As you see, there are 2 arrays in that one. Another problem is that new arrays gets added onto http.responseText so somehow I need to get rid of those that I have already processed so that I can process only the new ones that I haven't processed yet.

Example, log 2 looks like this:

{"type":1,"name":"String1","id":"1000004"}
{"type":1,"name":"String2","id":"60220"}
{"type":1,"name":"String3","id":"5743636"}
{"type":1,"name":"String4","id":"8555983"}
{"type":1,"name":"String5","id":"7732"}
{"type":1,"name":"String6","id":"92257"}

Any ideas??

::::EDIT::::

Solved it! Did the following..

PHP:

echo json_encode( array( 'type' => 1, 'name' => $stringVar, 'id' => $id ) ) . '%#%';

Notice the '%#%' at the end.

Javascript:

var lastResponse = '';
function useHttpResponse()
{

    if(http.readyState == 3)
    {

         // Get the original response before we edit it
         var originalResponse = http.responseText;
         // Replace the found last response in our original response with nothing(basically editing out the last response)
         var newResponse = originalResponse.replace( lastResponse, '' );
         // Add our new response to the last response
         lastResponse += newResponse;

         var responses = newResponse.split( "%#%" );
         $.each(responses, function(index, value){

              if( value != '' )
              {

                   var textout = eval( '(' + value + ')' );
                   console.log( 'Name: ' + textout.name + ', ID: ' + textout.id );

              }

        });

    }

}

Working excellent! :)

2 Answers 2

2

Well, you're echoing individual json chunks. That's why it's not working.. multiple json chunks in the same output isn't valid, so anything that interprets json is going to barf. Instead, add those arrays to a 'master' array and output that at the end of the script. For example,

$array = array();
while(loop) {
    array_push($array, array( 'type' => 1, 'name' => $stringVar, 'id' => $id ));
}

echo json_encode($array);

That should give you something like...

[
{"type":1,"name":"String1","id":"1000004"},
{"type":1,"name":"String2","id":"60220"},
{"type":1,"name":"String3","id":"5743636"},
{"type":1,"name":"String4","id":"8555983"},
{"type":1,"name":"String5","id":"7732"},
{"type":1,"name":"String6","id":"92257"}
]

which is valid

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

4 Comments

Unfortunately the main PHP-loop takes about 7 minutes to run and I'd like it to send back data continually instead of 1 big "chunk" after 7 minutes. Would you solution work in any case?
Unfortunately if you want to send back data continuously, json probably isn't the way to go. It's a very structured setup that, as far as I know, doesn't have any kind of 'stream in progress' format. You may be better off doing a custom format (i.e. ----\n$jsondata\n----) that you can read in via js, (i.e. the last ---\n(data)\n----) then decoding the contents of each chunk. Does that make any sense?
I solved it! :) Edited the first post, feel free to comment if it's a bad method. It is working perfectly though.
Hey, looks good to me. That's pretty much what I meant by the custom format. Good job
0

put your arrays in an other array so you have a 2 dimensional array... then make it json

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.