最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - xmlhttp.open("GET","ajax_info.txt",true): What am I missing? - Stack Overflow

programmeradmin1浏览0评论

According to w3schools the open method of the XMLHttpRequest object takes three parameters: 1. method 2. url 3. a boolean indicating whether the call is asynchronous or not

One of the examples used a text file for the url parameter. I copied the code and replaced the text file with my own and nothing happened. What am I missing?

This is the code I copied:

    <!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==4 && xmlhttp.status==200){
            document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
     }
    xmlhttp.open("GET","ajax_info.txt",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>

According to w3schools. the open method of the XMLHttpRequest object takes three parameters: 1. method 2. url 3. a boolean indicating whether the call is asynchronous or not

One of the examples used a text file for the url parameter. I copied the code and replaced the text file with my own and nothing happened. What am I missing?

This is the code I copied:

    <!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==4 && xmlhttp.status==200){
            document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
     }
    xmlhttp.open("GET","ajax_info.txt",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>
Share Improve this question edited Oct 5, 2021 at 9:51 Abdullah Akhtar 5585 silver badges14 bronze badges asked Sep 22, 2014 at 8:26 MR1971MR1971 191 gold badge1 silver badge4 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

You should put it inside some server and try.As we are using GET request here so it needed server. I have tried putting it inside wamp its working for me.

XMLHttpRequest is an Http Request :O

It's not called XMLLocalFileRequest :)

try using http://localhost/ajax_info.txt as a url .. a filename by itself is not a url.

Have you placed file ajax_info.txt in directory ? I edit a file as ajax_info.txt in directory with following content and it works perfectly.

<h1>AJAX</h1>
<p>AJAX is not a programming language.</p>
<p>AJAX is a technique for accessing web servers from a web page.</p>
<p>AJAX stands for Asyncronous JavaScript And XML.</p>

ajax_info.txt file present in w3school server. So thats why you cant access through it using your program.

Look this url: [https://www.w3schools./js/ajax_info.txt]

You need to use local server.

I use xampp as my local server, so I will try to explain in respect of that. If you are using some other local server check for that on google..

Put your files in a folder named (jscalls) in (htdocs) folder of xampp installed on your local machine. Then call

localhost/jscalls/nameOfFile.html

Now when you will click on the button it will get the content in the specified id.

For security reasons, modern browsers do not allow access across domains.

This means that both the web page and the XML file it tries to load, must be located on the same server.

The examples on W3Schools all open XML files located on the W3Schools domain.

If you want to use the example above on one of your own web pages, the XML files you load must be located on your own server.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论