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);
            }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab或python实现多个表格合并单元格
  • ¥15 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
  • ¥15 计算决策面并仿真附上结果
  • ¥20 halcon 图像拼接
  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥100 求ASMedia ASM1184e & ASM1187e 芯片datasheet/规格书
  • ¥15 求解决扩散模型代码问题