If you want to leave the user on the current page and make a new HTTP request, you must send the new request in JavaScript, and then update the page with JavaScript after receiving the data.
In this way, the user feels that they are still on the current page, but the data can be updated continuously.
An AJAX request is executed asynchronously, that is, a response is obtained through a callback function.
varRequest =NewXMLHttpRequest ();//new XMLHttpRequest ObjectRequest.onreadystatechange=function() {//function is callback when state changes if(Request.readystate = = 4) {//successfully completed //To determine the response result: if(Request.status = = 200) { //successful, get the text of the response via ResponseText: returnsuccess (Request.responsetext); } Else { //failure, according to the response code to determine the reason for failure: returnfail (request.status); } } Else { //the HTTP request is still continuing ... }}//Send request:Request.open (' GET ', '/api/categories '); Request.send (); Alert (' Request sent, please wait for response ... ');
JavaScript Learning-ajax