weixin_33739646 2018-08-10 22:29 采纳率: 0%
浏览 36

我的Ajax无法正常工作

I started to learn Ajax today. And I tried to do some practices by following step by step how to use AJAX but unfortunaly it's not working and I dont know where's the problem since I don't get any warning in console.log.

I have an example.xml in my root folder that i try to get data from.

Here's what I did. Thanks!

        function getXMLHttpRequest(){
    var xhr = null;
    
    if(window.XMLHttpRequest || window.ActiveXObject){
        if(window.ActiveXobject){
            try{
                xhr = new ActiveXobject("Msxml2.XMLHTTP");
            }catch(e){
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }else{
            xhr = new XMLHttpRequest();
        }
    
    }else{
        alert("Votre navigateur ne supporte pas l'objet XMLHttpRequest");
    }
    
    return xhr;
}


function request(callback){

    var xhr = getXMLHttpRequest();
    
    xhr.onreadystatechange = function(){
        if(xhr.readyStae == 4 && (xhr.status == 200 || xhr.status == 0)){
            callback(xhr.responseXML);
        }
    }
    
    xhr.open("GET", "example.xml", true);
    xhr.send(null);    
}

    function readData(oData){
        var node = oData.getElementsByTagName("soft");
        var ul = document.createElement("ul");
        var li, content;
        
        for(let i = 0; i < node.length; i++){
        
            li = document.createElement("li");      
            content = document.createTextNode(node[i].getAttribute("name"));
            li.appendChild(content);
            ul.appendChild(li);
        }


        document.getElementById("output").appendChild(ul);
    }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ajax</title>
</head>
<body>
    <p>
        <button onclick="request(readData);">Afficher les données</button>
    </p>
    <div id="output">
        
    </div>
</body>
</html>

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33726318 2018-08-10 22:31
    关注

    You have a typo in your code.

    xhr.onreadystatechange = function(){
            if(xhr.readyStae == 4 && (xhr.status == 200 || xhr.status == 0)){
                callback(xhr.responseXML);
            }
    }
    

    It should be xhr.readyState == 4, not xhr.readyStae == 4.

    xhr.onreadystatechange = function(){
            if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)){
                callback(xhr.responseXML);
            }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组