views:

61

answers:

3

Hi,

I'm just wondering..is it possible to receive multiple responses from a single ajax call? I'm thinking purely for aesthetic purposes to update the status on the client side.

I have a single ajax method that's called on form submit

$.ajax({
  url: 'ajax-process.php',
  data: data,
  dataType: 'json',
  type: 'post',
  success: function (j) {
  }
});

I can only get one response from the server-side. Is it possible to retrieve intermittent statuses? Such as:

Default (first): Creating account Next: Sending email confirmation Next: Done

Thanks for your help! :)

+1  A: 

From a single ajax call, I don't think it is possible.

What you could do is check frequently where the process is (it's what is used for the upload bars in gmail for example). You do a first ajax request to launch the process, and then a series of ajax request to ask the server how he is doing. When the server answers "I'm done", you're good to go, and until that you can make the server respond and say the current state.

Valentin Rocher
The nature of HTTP dictates that you can only pull data from the client - you can't push data from the server to the client
Jaco Pretorius
hmmm, yeah, that's exactly what I was saying, pulling data from the server via many ajax requests
Valentin Rocher
A: 

There is something called comet which you can set up to "push" requests to client, however it is probably way more than what you are wanting to invest in, time-wise.

You can open up a steady stream from the server, so that it continues to output, however I'm not sure how client-side script can handle these as individual "messages". Think about it like a server that outputs some info to the browser, does more work, outputs some more to the browser, does more work, etc. This shows up more or less in real time to the browser as printed text. It is one long response, but it is still one response. I think ajax only handles a response once it finished being sent, but maybe someone else will know more than me on the topic.

But you couldn't have the server output several individual responses without reloading itself, at least not with PHP, because once you start outputting the response, the response has begun and you can't chop that up without finishing the response, which happens when the script is done executing.

Your best bet is with the steady stream, but again, I'm not sure how ajax handles getting responses in chunks.

Quick Update

Based on the notes for this plugin:

[http://plugins.jquery.com/project/ajax-http-stream]

things don't look promising. Specifically:

Apparently the trend is to disallow access to the xmlhttprequest.responseText before the request is complete (stupid imo). Sorry there's nothing I can do to fix this

Thus, not only can you not get what you want in one request, you probably can't get it multiple requests, unless you want to break up the actual server-side process into several parts, and only have it continue to the next step when an ajax function triggers it.

Another option would be to have your script write it's status at specific points to another file on the server, call it "status.xml" or "status.txt". Have your first ajax function initialize the process, and have a second ajax function that queries this status file and outputs that to the user.

Anthony
thanks Anthony. The plugin would have done what I wanted i think. :) +1 vote
Lyon
A: 
iangraham
This is interesting. Never considered sleep() before. :) Thanks iangraham! I was hoping there would be something simpler since it's purely for aesthetic purposes. The information from you all will definitely help next time when i need to execute longer server processes. Thanks guys! :)
Lyon