Home > Backend Development > PHP Tutorial > How to implement simple ajax Loading function in PHP

How to implement simple ajax Loading function in PHP

墨辰丷
Release: 2023-03-28 09:58:02
Original
1838 people have browsed it

This article mainly introduces the method of implementing simple ajax Loading loading function in PHP. It analyzes the principle, operation skills and related precautions of ajax loading in the form of examples. Friends in need can refer to it

The details are as follows :

var xmlHttp;
function createXmlHttpReq() {
  if(window.ActiveXObject) {
    xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
  } else if(window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
  }
}
function funMy(url) {
  createXmlHttpReq();
  try {
    xmlHttp.onreadystatechange = cb;//一定要在open()前,下边会有说明。在此处犯错了
    xmlHttp.open("GET","for.php?id="+url,true);
    xmlHttp.send(null);
  } catch(e) {
    alert("您访问的资源不存在");
  }
}
//回调函数
function cb() {
  if(xmlHttp.readyState==1) {
    alert("1-------------->");
    //在Google Chrome 浏览器里不显示loading图片,三秒后显示内容,问题已解决,下边有说明
    document.getElementById(&#39;ajax&#39;).innerHTML = "<img src=loading2.gif>";
    //document.getElementById(&#39;ajax&#39;).innerHTML = "Loading......";
  }
  if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
      var data = xmlHttp.responseText;
      document.getElementById(&#39;ajax&#39;).innerHTML = data;
  }
}
Copy after login

When I was testing, I got stuck on Chrome. Please see the explanation below:

If you write it this way, you will not receive a response of .readyState==1

Because 1 means that .open() has been called and completed

But .open() is called before the .onreadystatechange event, so it should be impossible for you to receive a response of .readyState==1

Therefore, if you want to receive .readyState==1 =>.onreadystatechange, you must Before .open()
So why do you sometimes receive it?

Because you use the same global variable...During continuous operations, it may happen because an xhr request is still waiting for php. To initialize it again

You should first decide on the data processing method onreadystatechange, and then send the data to be processed open()

The above is the entire content of this article, I hope it will be helpful to Everyone’s learning helps.


Related recommendations:

Configuration of the Ajax global loading box (Loading effect)

Ajax returns data before loading Waiting effect (graphic tutorial)

Ajax implements Loading effect

The above is the detailed content of How to implement simple ajax Loading function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template