使用jQuery读取文本文件 [英] Reading a text file with jQuery

查看:80
本文介绍了使用jQuery读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

jquery - 读取文本文件?


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

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

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



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

解决方案

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

  $。get('file_to_read.txt',function(data){
do_something_with(data)
},'text');
// ^ ------最后一个参数

否则jQuery会猜到什么是返回。






记住, $。获取只是方便 $。ajax 的包装器。数据类型列在 $。ajax() docs 中...


dataType



默认值:智能猜测(xml,json,script或html)



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



xml:返回可以通过jQuery处理的XML文档。 / p>

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可以转换数据类型,而不是从内容中收到的数据类型输入标题即可。例如,如果要将文本响应视为XML,请对dataType使用text xml。您还可以发出JSONP请求,将其作为文本接收,并由jQuery解释为XML:jsonp text xml。类似地,诸如jsonp xml之类的速记字符串将首先尝试从jsonp转换为xml,如果失败,则从jsonp转换为text,然后从text转换为xml。



Possible Duplicate:
jquery - Read a text file?

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

$.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.

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?

解决方案

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

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

Otherwise jQuery guesses at what was returned.


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

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.

这篇关于使用jQuery读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆