简体   繁体   中英

Sample AJAX call not working

Trying to do a simple AJAX call (teaching myself). I have a .txt file in same folder as html . What am I doing wrong here? Thanks.

<html>
   <head>
      <script type="text/javascript">
        $(document).ready(function(){
            $("#poo").submit(function(e){
                e.preventDefault(); //stop submit
                $.ajax({
                    type: "GET",
                    url: "data.txt",
                    data: "", 
                    success: function(data){
                        $("#foo").html(data);
                        document.getElementById("foo").style.display = 'block';
                        alert('hey');
                    }
                });
            });
        });
    </script>
   </head>
    <body>
        <form id="poo">
            <input type="text"> </input>
            <input type="submit"> </input>
        </form>
            <br>
            <br>
            <div style='display: none;'>
            <p id="foo">this shows</p>
            </div>
            <a href="page.html">Start Over</a>


    </body>
  </head>
</html>

This is a convenience function that loads remote files via AJAX and uses .innerHTML() to load it into any elements in your jQuery selector.

// Does the exact same thing as the entire block of code you wrote..
// These jQuery methods are chainable so you can do this in 1 statement.

$("#foo")                // Contains the DOM reference, 
                         // so no need to use getElementById().
    .load("data.txt")    // Loads "data.txt" into "#foo". 
    .show();             // Makes "#foo" visible.

Relevant:


Per your comments, you had some other issues.

You said you weren't sure if jQuery was loaded. jQuery is just javascript, so you include it in <script></script> tags. The easiest way is to use jQuery's CDN . Click on the link, then choose the version and format you want. There will be a pop-up containing the script tag, just copy it into the page, preferably before any other Javascript you have on the page. If you're not sure which version/format to use, v1.x, minified is the way to go.


You mentioned that you were running it locally. The problem is, Javascript isn't supposed to have direct access to your filesystem. It will attempt to request the file over the http protocol, without having server software you can only request files over the file:// protocol.

There are zillions of topics on this all over the internet, but to solve it you should install a server. XAMPP is a good one and it's cross platform. Download that and your application will work in all your browsers. It will work in your browsers when you upload it to a server as well

try to add the dataType: "text"

$.ajax({
    type: "GET",
    url: "data.txt",
    dataType: "text",
    success: function(data){
        $("#foo").html(data);
        document.getElementById("foo").style.display = 'block';
        alert('hey');
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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