Javascript jQuery 加载文本文件数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6338797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:18:37  来源:igfitidea点击:

jQuery to load text file data

javascriptjquery

提问by kingrichard2005

I'm trying to load data from a text file on my server using the $.get() function in an external script file. My code is as follows:

我正在尝试使用外部脚本文件中的 $.get() 函数从我服务器上的文本文件加载数据。我的代码如下:

  /* 
   * Load sample date
   */
  var stringData;
  $.get("http://localhost/webpath/graphing/sample_data.txt", function(data){
      stringData = data;
      //alert("Data Loaded: " + stringData);
      });
  //Split values of string data
  var stringArray = stringData.split(",");
  alert("Data Loaded: " + stringData);

When I'm inside the $.get() function I can see stringData var get peopulated just fine and the call to alert confirms that it contains data from the sample text file. However, when I get outside the $.get() function, the stringData var no longer shows. I don't know enough about how the function works to know why it is not doing as I expected. All I want it to do is load the text data into a variable so I can work with it. Any help is appreciated.

当我在 $.get() 函数中时,我可以看到 stringData var get peopulated 就好了,对 alert 的调用确认它包含来自示例文本文件的数据。但是,当我离开 $.get() 函数时,stringData var 不再显示。我对该函数的工作原理知之甚少,无法知道为什么它没有按我预期的那样工作。我想要它做的就是将文本数据加载到一个变量中,以便我可以使用它。任何帮助表示赞赏。

回答by Praveen Lobo

get is asynchronous meaning it makes a call to the server and continues executing the rest of the code. This is why you have callback methods. Whatever you intend to do with the return data do it inside the callback method(where you have put the alert).

get 是异步的,这意味着它调用服务器并继续执行其余代码。这就是为什么你有回调方法。无论您打算对返回数据做什么,都可以在回调方法(放置警报的地方)中进行。

get, post are all asynchronous. You can make a synchronous call by using

get、post都是异步的。您可以通过使用进行同步调用

  1. using $.ajaxSetup({ async: false });anywhere in your code. This will affect all ajax calls in your code, so be careful.

  2. $.ajaxwith async: falsee.g. shown below.

  1. $.ajaxSetup({ async: false });在代码中的任何地方使用。这将影响您代码中的所有 ajax 调用,所以要小心。

  2. $.ajaxasync: false例如如下所示。

Look at the below code and the API docs link mentioned above to fix your problem.

查看下面的代码和上面提到的 API 文档链接来解决您的问题。

  /* 
   * Load sample date
   */
  var stringData = $.ajax({
                    url: "http://localhost/webpath/graphing/sample_data.txt",
                    async: false
                 }).responseText;

  //Split values of string data
  var stringArray = stringData.split(",");
  alert("Data Loaded: " + stringData);

回答by joekarl

The $.get function is asynchronous. You'll need to do any work on the returned data in the callback function. You can move that function to not be inline to clean up the code as well.

$.get 函数是异步的。您需要对回调函数中返回的数据进行任何处理。您也可以将该函数移至不内联以清理代码。

    function parseData(data){
        //do something with the data
        alert("data is: " + data);
    }

    $.get("http://localhost/webpath/graphing/sample_data.txt",parseData);