weixin_33709364 2014-09-12 12:58 采纳率: 0%
浏览 17

Ajax不会加载简单文件

I started learning AJAX and got problem on the beginning I can't solve. I got 2 files, main.html with code :

<!DOCTYPE html>
<html>
<head>
    <title>My page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <button>apple</button>
    <div id="target">
    Press button
    </div>

    <script>
    var buttons = document.getElementsByTagName("button");
    for (var i = 0; i < buttons.length; i++)
    {
        buttons[i].onclick = handleButtonPress;
    }

    function handleButtonPress(e)
    {

        var xmlhttp = new XMLHttpRequest();

        xmlhttp.open("GET","blabla.txt",false);
        xmlhttp.send(null);

        document.getElementById("target").innerHTML=xmlhttp.responseText;

    }
</script>
</body>
</html>

and "blabla.txt" with content :

asdasdsaldkjasdajsdl

Problem is that after clicking the button it should load content of blabla.txt file into the div element. Unfortunately it doesnt work with the reason i have no idea about.

I think its worthly to add that both files are placed in the same folder.

  • 写回答

3条回答 默认 最新

  • weixin_33690963 2014-09-12 13:02
    关注
    xmlhttp.onreadystatechange=function()
     {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
         document.getElementById("target").innerHTML=xmlhttp.responseText;
      }
     }
    
    评论

报告相同问题?