How to load the content of a file into variable using jquery load method?

前端 未结 2 1516
闹比i
闹比i 2021-02-05 02:32

How do I load the content of a file into a variable instead of the DOM using jQuery .load() method?

For example,

$(\"#logList\").load(\"logF         


        
2条回答
  •  借酒劲吻你
    2021-02-05 03:18

    You can use $.get() to initiate a GET request. In the success callback, you can set the result to your variable:

    var stuff;
    $.get('logFile', function (response) {
        stuff = response;
    });
    

    Please note that this is an asynchronous operation. The callback function will run when the operation is completed, so commands after $.get(...) will be executed beforehand.

    That's why the following will log undefined:

    var stuff;
    $.get('logFile', function (var) {
        stuff = var;
    });
    console.log(stuff); //undefined
    

提交回复
热议问题