Javascript 使用 jQuery 读取文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10112509/
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 23:53:36  来源:igfitidea点击:

Reading a text file with jQuery

javascriptjquery

提问by Gadi A

Possible Duplicate:
jquery - Read a text file?

可能重复:
jquery - 读取文本文件?

I want to read a local text file, using jQuery. So I try this:

我想使用 jQuery 读取本地文本文件。所以我试试这个:

$.get('file_to_read.txt', function(data) {
   do_something_with(data)
});

However, jQuery interprets "file_to_read.txt" as an html file and I get a Javascript error because it's not properly formatted and "do_something_with" does not have its desired effect, since data is not a string.

但是,jQuery 将“file_to_read.txt”解释为一个 html 文件,我收到一个 Javascript 错误,因为它的格式不正确并且“do_something_with”没有达到预期的效果,因为数据不是字符串。

the jQuery doc says I need to specify the datatype. However, they only list html, xml, json and script as the possible data files; what should I do with a plain txt file I want to load directly into a string?

jQuery 文档说我需要指定数据类型。但是,他们只列出了 html、xml、json 和 script 作为可能的数据文件;我应该如何处理我想直接加载到字符串中的纯 txt 文件?

回答by

Use 'text'datatype in your $.get()request.

'text'在您的$.get()请求中使用数据类型。

$.get('file_to_read.txt', function(data) {
   do_something_with(data)
}, 'text');
 //  ^------last argument

Otherwise jQuery guesses at what was returned.

否则 jQuery 会猜测返回的内容。



Remeber, $.getis just a convenience wrapper for $.ajax. The datatypes are listed in the $.ajax()docs...

请记住,$.get它只是$.ajax. 数据类型列在$.ajax()文档中...

dataType

Default: Intelligent Guess (xml, json, script, or html)

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

"xml": Returns a XML document that can be processed via jQuery.

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

"script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests.

"json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

"jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.

"text": A plain text string.

multiple, space-separated values: As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

数据类型

默认:智能猜测(xml、json、脚本或 html)

您期望从服务器返回的数据类型。如果没有指定,jQuery 将尝试根据响应的 MIME 类型推断它(XML MIME 类型将产生 XML,在 1.4 中 JSON 将产生一个 JavaScript 对象,在 1.4 脚本中将执行脚本,其他任何类型都将以字符串形式返回)。可用类型(以及作为第一个参数传递给成功回调的结果)是:

"xml": 返回一个可以通过 jQuery 处理的 XML 文档。

"html": 以纯文本形式返回 HTML;包含的脚本标签在插入 DOM 时进行评估。

“script”:将响应作为 JavaScript 进行评估,并将其作为纯文本返回。通过将查询字符串参数“_=[TIMESTAMP]”附加到 URL 来禁用缓存,除非缓存选项设置为 true。注意:这会将 POST 转换为远程域请求的 GET。

“json”:将响应评估为 JSON 并返回一个 JavaScript 对象。在 jQuery 1.4 中,JSON 数据以严格的方式解析;任何格式错误的 JSON 都会被拒绝并引发解析错误。(有关正确 JSON 格式的更多信息,请参阅 json.org。)

“jsonp”:使用 JSONP 加载到 JSON 块中。添加一个额外的“?callback=?” 到 URL 的末尾以指定回调。通过将查询字符串参数“_=[TIMESTAMP]”附加到 URL 来禁用缓存,除非缓存选项设置为 true。

“text”:纯文本字符串。

多个空格分隔的值:从 jQuery 1.5 开始,jQuery 可以将 dataType 从它在 Content-Type 标头中收到的内容转换为您需要的内容。例如,如果您希望将文本响应视为 XML,请对 dataType 使用“text xml”。您还可以发出 JSONP 请求,将其作为文本接收,然后由 jQuery 解释为 XML:“jsonp text xml”。类似地,诸如“jsonp xml”之类的速记字符串将首先尝试从 jsonp 转换为 xml,如果失败,则从 jsonp 转换为文本,然后从文本转换为 xml。

回答by Gavriel

try dataType "text" (or "html", it should work if you don't have html "script" tags in it)

尝试 dataType "text" (或 "html",如果你没有 html "script" 标签,它应该可以工作)