AJAX - getting response after user changes pages

What i know:
When I make an ajax call on my server, a handler is created, the request is sent, my php script receives the request and processes it, and if I pass it, it sends a response that processes my javascript as I need. I also know that php will continue to process the request, even if the user closes the browser or changes pages (all this is done on the server side, so why not? ^. ^).

What I need to know:
Is an ajax handler killed when a user changes pages on my site? For example: the user is at mysite.com/foo.php. They click on the link that sends the ajax request to my server. The response of this request should be shown in div # resp on foo.php. Then they go to mysite.com/bar.php until they get a response.

If I load the same javascript functions and have the necessary div # resp element on bar.php, can the javascript function that called ajax receive a response from the server and pass it to div # resp on bar.php, thus showing the answer? Or is the original ajax pen no longer available? And if it is no longer available in standard javascript, is there some kind of implantation in jQuery that will allow me to get an answer and show it on bar.php?

I hope this is clear.

+7
source share
9 answers

From what you describe, no - as soon as you go to another page and cause the page to reload, all your javascript handlers are re-created and the original ones destroyed.

+7
source

The answer is no. When you change the page, the javascript process gets killed and restarts. There is nothing left between reloading the page.

Although, if your page also changes in ajax, then the process will not be killed, and you can get an answer. (BTW, this can be done mainly for the end user with PushState in the latest browser)

+5
source

No, your XHR (AJAX) response is not guaranteed before the user moves to the next page.

If you want to detect the server side when the user leaves your page, you will have to use WebSocket or similar technology. A WebSocket connection is a long-term connection to a foreign host from which you can easily find the server side when it is canceled. https://github.com/sockjs is an example of a WebSocket client + server.

If you just want to display something to the user before he or she moves away from the page, you need to look at window.onbeforeunload - onbeforeunload fires before the user leaves the page, but will not allow you to do any asynchronous AJAX Requests For more information see https://developer.mozilla.org/en-US/docs/Web/API/window.onbeforeunload

+2
source

If you want to do this, create a "one-page web application." Simple and simple, do not post a new URL.

Instead, use pushstate / popstate to change the address in the browser, and then use javascript / dynamic html to redraw the page.

Then you can process all ajax responses.

+1
source

Another option is to have an onload event that raises an ajax request to the server and asks if something needs to be done. The server will need to track "things" and let the browser know what it is doing.

In essence, the browser will poll the server at the beginning of each page.

I do not think that the sample code is necessary or applicable, because it will change for each application.

+1
source

You can save the values ​​to localStorage as they are received or save all responses to ajax data in your session, memcache or redis. This means that if the server receives the request, it first saves the response somewhere before returning it back. Removing a message / reply from the list will depend on your situation. Some systems allow you to close notifications that others disappear after the user first sees them. Disappearing disappears. The user may or may not see it.

Sounds like a flash message from different libraries. You can look at the implementation of these systems.

0
source

While the original accepted answer is correct - you cannot get an answer to the ajax request as soon as you go to another page - there is a workaround: if you make the site a single page application, you can get the answer.

0
source

I had the same situation in one of my projects. To have something transient between pages, you need to have something that you learn from one page to another. The simplest thing to use is PHP sessions (I saw that you are using PHP). Easy enough, you can download Async Request (Ajax) on each page, which will check on the server (using PHPSESSID), if there is activity. You have the opportunity to check the loading of each page (at docready) or at intervals.

Of course, you lose everything if the user closes the browser tabs.

0
source

HOW YOU CAN GET A PAGE ALL AJAX CALLS ARE NOT AVAILABLE FOR A PAGE IF A SIMPLE HTTP POST IS USED

The HTTP request life cycle usually looks like this:

The user visits the website URL. This creates a request that is sent to the web server via the Internet (DNS network, routers and switches) via HTTP (Hypertext Transfer Protocol). The web server receives an HTTP request and responds to the user with the requested web page (or content). Each time you click on a link and visit a web page, behind the scenes you make a request and, in turn, get a response from the web server. Please note that HTTP requests can be executed through many channels, and not just through web browsers. For example, an HTTP request can be made using TELNET or a client written in JAVA or C #, etc.

Therefore, you should use sockets for this so that the http life cycle can be changed.

http://devhub.fm/http-requestresponse-basics/

0
source

All Articles