Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
Jquery 极其简单的ajax XmlHttpRequest代码不起作用_Jquery_Ajax_Xmlhttprequest - Fatal编程技术网

Jquery 极其简单的ajax XmlHttpRequest代码不起作用

Jquery 极其简单的ajax XmlHttpRequest代码不起作用,jquery,ajax,xmlhttprequest,Jquery,Ajax,Xmlhttprequest,我刚刚开始在W3Schools网站上学习ajax。在页面上,当我尝试以下代码时,我得到了正确的响应,正如人们所期望的那样。您也可以尝试代码,只需复制并粘贴到给定的链接页面“提交代码”,单击响应中的“更改内容”按钮 <!DOCTYPE html> <html> <head> <script> function loadXMLDoc() {

我刚刚开始在W3Schools网站上学习ajax。在页面上,当我尝试以下代码时,我得到了正确的响应,正如人们所期望的那样。您也可以尝试代码,只需复制并粘贴到给定的链接页面“提交代码”,单击响应中的“更改内容”按钮

 <!DOCTYPE html>
    <html>
        <head>
            <script>
                function loadXMLDoc()
                {
                    var xmlhttp;
                    if (window.XMLHttpRequest)
                    {// code for IE7+, Firefox, Chrome, Opera, Safari
                        xmlhttp=new XMLHttpRequest();
                    }
                    else
                    {// code for IE6, IE5
                        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    xmlhttp.onreadystatechange=function()
                    {
                        if (xmlhttp.readyState==0)
                        {
                            document.getElementById("myDiv").innerHTML="0: request not initialized";
                        }if (xmlhttp.readyState==1)
                        {
                            document.getElementById("myDiv").innerHTML="Server connection establish";
    
                        }if (xmlhttp.readyState==2)
                        {
                            document.getElementById("myDiv").innerHTML="Request has been sent";
                        }if (xmlhttp.readyState==3)
                        {
                            document.getElementById("myDiv").innerHTML="Server is processing the request";
                        }
                        if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        {
                            document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                        }
                    }
                    xmlhttp.open("GET","http://www.w3schools.com/ajax/ajax_example.asp",true);
                    xmlhttp.send();
                }
            </script>
        </head>
        <body>
    
            <div id="myDiv"><h2>Let AJAX change this text</h2></div>
            <button type="button" onclick="loadXMLDoc()">Change Content</button>
    
        </body>
    </html>

函数loadXMLDoc()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==0)
{
document.getElementById(“myDiv”).innerHTML=“0:请求未初始化”;
}if(xmlhttp.readyState==1)
{
document.getElementById(“myDiv”).innerHTML=“服务器连接建立”;
}if(xmlhttp.readyState==2)
{
document.getElementById(“myDiv”).innerHTML=“请求已发送”;
}if(xmlhttp.readyState==3)
{
document.getElementById(“myDiv”).innerHTML=“服务器正在处理请求”;
}
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“myDiv”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”http://www.w3schools.com/ajax/ajax_example.asp“,对);
xmlhttp.send();
}
让AJAX更改此文本
更改内容
但是,当我在自己的html文件中尝试相同的代码并单击“更改内容”按钮时,我会按此顺序更改内容(请不要输入错误)

  • 已建立服务器连接
  • 请求已发送

之后什么也没有发生。我已连接到Internet,也可以在浏览器(Firefox)中打开链接。我该怎么办?

您不能从本地html文件中编写的代码使用ajax访问另一个域地址。跨域ajax请求要求您使用
jsonp
访问该数据


在第一种情况下,由于您在发出ajax请求时已经在w3schools域内,因此它将起作用。

thanx redDevil可帮助您快速响应。