简体   繁体   English

简单的ajax什么也没有发生

[英]simple ajax nothing happens

I trying to create a small ajax script that add some text into div. 我试图创建一个小的Ajax脚本,将一些文本添加到div中。 nothing happen, it's killing me. 什么都没发生,这正在杀死我。

please help. 请帮忙。

HTML: HTML:

    <!DOCTYPE>
    <html>

    <head>
                <script  type="text/javascript" src="ajax.js"></script>
                <script  type="text/javascript" src="jquery.js"></script>   
    </head>

    <body onload="process()">
        OK, you made it this far
        <br/>
        <div id="theD">
        </div>

    </body>

    </html>

ajax.js: ajax.js:

    var xmlHttp= createXmlHttpRequestObject();

    function createXmlHttpRequestObject(){
        var xmlHttp;

        if (window.XMLHttpRequest)(
            xmlHttp = new XMLHttpRequest();
        )else{
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
        }

        return xmlHttp;
    }


    function process(){
        alert('hi');

        if (xmlHttp){
            try{
                xmlHttp.open("GET", "ajax.txt", true);
                xmlHttp.onreadystatechange = handleServerResponse;
                xmlHttp.send(null);

            }catch(e){
                alert(e.toString());

            }
        }
    }

    function handleServerResponse(){
        theD = documet.getElementById('theD');

        if (xmlHttp.readyState==1){
            theD.innerHTML += "Status1:server connection established <br/>";

        }else if (xmlHttp.readyState==4){
            if (xmlHttp.status=200){
                try{
                    text=xmlHttp.responseText
                    theD.innerHTML += "Status4:request finish<br/>";
                    theD.innerHTML += text;
            }catch(e){
                alert(e.toString);

            }

            }else{
                alert((xmlHttp.statusText);
            }
        }

    }

the ajax.txt contain a simple string. ajax.txt包含一个简单的字符串。

this is xhr2 if you want more browser support you can extend it. 如果您想获得更多浏览器支持,则可以将其扩展为xhr2。

http://caniuse.com/xhr2 http://caniuse.com/xhr2

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ajax</title>
<script>
function ajax(a,b,c){ // Url, Callback, just a placeholder
 c=new XMLHttpRequest;
 c.open('GET',a);
 c.onload=b;
 c.send()
}
function h(){
 document.getElementById('theD').innerText=this.response
}
window.onload=function(){
ajax('ajax.txt',h);
}
</script>
</head>
<body>
<div id="theD"></div>
</body>
</html>

if you have any questions about how this works or how you can extend it just ask 如果您对它如何工作或如何扩展有任何疑问,请问

here you have some more info about this 在这里,您有关于此的更多信息

https://stackoverflow.com/a/18309057/2450730 https://stackoverflow.com/a/18309057/2450730

you can add ie support 您可以添加支持

by replacing 通过替换

c=new XMLHttpRequest;

with

c=new XMLHttpRequest||new ActiveXObject("MSXML2.XMLHTTP.3.0");

and using onreadystatechange 并使用onreadystatechange

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM