Load .txt file using JQuery or Ajax

后端 未结 4 605
南笙
南笙 2020-12-02 00:07

How can I fix the script below so that it will work EVERY TIME! Sometimes it works and sometimes it doesn\'t. Pro JQuery explains what causes this, but it doesn\'t

相关标签:
4条回答
  • 2020-12-02 00:31

    Try this:

    HTML:

    <div id="target"></div>
    

    JavaScript:

    $(function(){
        $( "#target" ).load( "pathToYourFile" );
    });
    

    In my example, the div will be filled with the file contents. Take a look at jQuery .load() function.

    The "pathToYourFile" cand be any resource that contains the data you want to be loaded. Take a look at the load method documentation for more information about how to use it.

    Edit: Other examples to get the value to be manipulated

    Using $.get() function:

    $(function(){
        $.get( "pathToYourFile", function( data ) {
            var resourceContent = data; // can be a global variable too...
            // process the content...
        });
    });
    

    Using $.ajax() function:

    $(function(){
        $.ajax({
            url: "pathToYourFile",
            async: false,   // asynchronous request? (synchronous requests are discouraged...)
            cache: false,   // with this, you can force the browser to not make cache of the retrieved data
            dataType: "text",  // jQuery will infer this, but you can set explicitly
            success: function( data, textStatus, jqXHR ) {
                var resourceContent = data; // can be a global variable too...
                // process the content...
            }
        });
    });
    

    It is important to note that:

    $(function(){
        // code...
    });
    

    Is the same as:

    $(document).ready(function(){
        // code
    });
    

    And normally you need to use this syntax, since you would want that the DOM is ready to execute your JavaScript code.

    0 讨论(0)
  • 2020-12-02 00:36

    Try this:

    var pageExecute = {
    
        fileContents:"Null",
        pagePrefix:"Null",
        slides:"Null",
    
        init: function () {
            $.ajax({
                url: "./seeds/Ag.txt",
                async: false,
                success: function (data){
                    pageExecute.fileContents = data;
                }
            });
        }
    };
    
    0 讨论(0)
  • 2020-12-02 00:45

    Here's your issue: You've got a script tag in the body, which is asking for the AJAX data. Even if you were asking it to write the data to your shell, and not just spout it... ...that's your #1 issue.

    Here's why:

    AJAX is asynchronous. Okay, we know that already, but what does that mean?

    Well, it means that it's going to go to the server and ask for the file. The server is going to go looking, and send it back. Then your computer is going to download the contents. When the contents are 100% downloaded, they'll be available to use.

    ...thing is...

    Your program isn't waiting for that to happen. It's telling the server to take its time, and in the meantime it's going to keep doing what it's doing, and it's not going to think about the contents again, until it gets a call from the server.

    Well, browsers are really freakin' fast when it comes to rendering HTML. Servers are really freakin' fast at serving static (plain-text/img/css/js) files, too.

    So now you're in a race. Which will happen first? Will the server call back with the text, or will the browser hit the script tag that asks for the file contents?

    Whichever one wins on that refresh is the one that will happen.

    So how do you get around that? Callbacks.

    Callbacks are a different way of thinking. In JavaScript, you perform a callback by giving the AJAX call a function to use, when the download is complete.

    It'd be like calling somebody from a work-line, and saying: dial THIS extension to reach me, when you have an answer for me.

    In jQuery, you'll use a parameter called "success" in the AJAX call. Make success : function (data) { doSomething(data); } a part of that object that you're passing into the AJAX call. When the file downloads, as soon as it downloads, jQuery will pass the results into the success function you gave it, which will do whatever it's made to do, or call whatever functions it was made to call.

    Give it a try. It sure beats racing to see which downloads first.

    0 讨论(0)
  • 2020-12-02 00:47

    I recommend not to use url: "./seeds/Ag.txt",, to target a file directly. Instead, use a server side script llike PHP to open the file and return the data, either in plane format or in JSON format.

    You may find a tutorial to open files here: http://www.tizag.com/phpT/fileread.php

    0 讨论(0)
提交回复
热议问题