1

I need to initiate a reload from the server side(PHP) durring and AJAX call. How do I do this. I honestly don't think it is supported.

My current method is to use the PHP header() function and capture it as responseText from the AJAX call and then do an innerHTML add.

However it is not a proper reload in my book as it doen not intiate the onload function. It also seems to to be contrived and not a "clean" solution.

casperOne
  • 73,706
  • 19
  • 184
  • 253

3 Answers3

2

Send a command/flag back to your Ajax handler to make it call the following JavaScript method in the page:

window.location.reload();
Jon Benedicto
  • 10,492
  • 3
  • 28
  • 30
  • I would send a flag that tells the JS to reload the page. Otherwise you'll have to eval or inject script into the page. – aepheus Aug 02 '11 at 19:59
  • How does this not do what you asked? It should still fetch from the server. The only possible hiccup would be caching issues, where there are a few suggestions here: http://stackoverflow.com/questions/5721704/window-location-reload-with-clear-cache And why would you need the whole document loaded? The better option would be to use data from the AJAX call, or even formatted HTML, and repopulate the individual DOM elements. document.write()-ing the whole page is very bad, IMO – Richard Hoffman Aug 02 '11 at 20:17
  • I could just use innerHTML..I would not need to "eval or inject" –  Jan 05 '12 at 17:45
1

There is a pattern that uses AJAX to create a PUSH-alike application. I don't know the real name of the paradigm, nor a good implementation, but this is basically how it works:

  1. Client makes AJAX request to server.
  2. Server doesn't close the connection.
  3. When server wants to push. server says:
    a. Process push message X
    b. Initiate a new connection. (i.e. goto 1.)
    < Server closes connection >

So there is always a "hanging" connection, and as soon as the server wants to respond, the client immediately creates a new one that hangs again.

Pelle
  • 6,423
  • 4
  • 33
  • 50
  • 1
    That would be XHR long-polling. That is one of many techniques for doing a server push. [link](http://en.wikipedia.org/wiki/Comet_(programming)) – Richard Hoffman Aug 02 '11 at 20:21
  • I think this is more related to chat applications and similar –  Jan 05 '12 at 17:45
0

The only way to reload the entire page is to change the window.location. You can't do that from the server. Every action must be initiated from the client - that's fundamental of restful protocols like HTTP.

However, your server could send back the entire document, and you could then 'pick' the portion to replace. jQuery allows this with the load function.

For instance this would reload the BODY: $('body').load('ajax/page.html body');

Martin Drapeau
  • 1,484
  • 15
  • 16
  • I'm saying Jon Benedicto has the correct answer. Alternatively though, the server could return the whole document in an AJAX call. But when you receive it, only take parts you want - not all of it. Not sure if that would be useful or not. – Martin Drapeau Aug 05 '11 at 01:09