Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript XMLHttpRequest在代码中做了什么_Javascript_Xmlhttprequest - Fatal编程技术网

Javascript XMLHttpRequest在代码中做了什么

Javascript XMLHttpRequest在代码中做了什么,javascript,xmlhttprequest,Javascript,Xmlhttprequest,我是Javascript的初学者,我想了解XMLHttpRequest方法的功能 这是我正在阅读的代码,我想知道是否有人可以解释它在做什么: var xhttp; xhttp=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP"),xhttp.open("GET","script.php",!0),xhttp.send(); 它是对AJAX请求的引用。 看 简而言之,它向script.php

我是Javascript的初学者,我想了解XMLHttpRequest方法的功能

这是我正在阅读的代码,我想知道是否有人可以解释它在做什么:

var xhttp;
xhttp=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP"),xhttp.open("GET","script.php",!0),xhttp.send();

它是对AJAX请求的引用。 看


简而言之,它向script.php发送GET请求。

XMLHttpRequest是一个用于发出AJAX请求的JavaScript对象。我不能完全确定代码是否正确。通常创建XMLHttpRequest对象的实例。然后检查窗口就绪状态以发出请求。最后你提出请求。这是一个例子:

var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
        callback(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
我希望这有帮助


快乐编码

嗨,我不是很擅长解释,但我会在看到和理解这一点时,尝试详细解释这一点

XMLHttpRequest是一个对象。它用于与服务器交换数据。因此,使用它,您可以向服务器上的脚本发送一些数据(请求),并从中获取一些数据(响应)。响应数据可以立即显示在页面上,而无需重新加载页面。所以这个过程调用

我会把你提供的代码读作

//define a variable 
var xhttp;
/*assign a XMLHttpRequest object to this variable
check if the global object window has a XMLHttpRequest object already
if not and user have a newer browser, create one (new XMLHttpRequest - for           IE7+, Firefox, Chrome, Opera, Safari browsers) or user have an older browser (ActiveXObject("Microsoft.XMLHTTP") - for IE6, IE5 browsers)
xhttp.open method specifies the type of request(method GET, Script on server,    asynchronous)
xhttp.send method sends the request to a server*/

xhttp=window.XMLHttpRequest?new XMLHttpRequest:new     ActiveXObject("Microsoft.XMLHTTP"),xhttp.open("GET","script.php",!0),xhttp.send();
但是您还必须检查XMLHttpRequest对象的readyState属性

xmlhttp.onreadystatechange = function() {
    //4: request finished and response is ready
    //200: "OK"
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

        //display of returned data from the server
        //it is available in this property - xmlhttp.responseText
    }
}
整个代码的和平应该是这样的:

if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();                     // code for IE7+, Firefox, Chrome, Opera, Safari
} else {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");   // code for IE6, IE5
}
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

        //display of returned data from the server
        //jquery example
        $('div').html(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", "script.php", true);
xmlhttp.send();
希望这有帮助,祝你好运