XmlHttpRequest向服务器发送一个HTTP请求(POST | GET | ...),然后等待它的输出。在您的情况下,您将在服务器上调用php文件并等待其响应。
From Javascript.info
// 1. Create a new XMLHttpRequest object let xhr = new XMLHttpRequest(); // 2. Configure it: GET-request for the URL xhr.open('GET','/yourPhPfileLocationUrl/'); // 3. Send the request over the network xhr.send(); // 4. This will be called after the response is received xhr.Onload= function() { if (xhr.status != 200) { // analyze HTTP status of the response alert(`Error ${xhr.status}: ${xhr.statusText}`); // e.g. 404: Not Found } else { // show the result alert(`Done,got ${xhr.response.length} bytes`); // responseText is the server } }; xhr.Onprogress= function(event) { if (event.lengthComputable) { alert(`Received ${event.loaded} of ${event.total} bytes`); } else { alert(`Received ${event.loaded} bytes`); // no Content-Length } }; xhr.Onerror= function() { alert("Request failed"); };