22

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?

Community
  • 1
  • 1
Gadi A
  • 3,449
  • 8
  • 36
  • 54
  • Why you don't get it with HTML format and then convert it to string? – Afshin Mehrabani Apr 11 '12 at 19:23
  • Possible dulicate http://stackoverflow.com/questions/1981815/jquery-read-a-text-file – coder Apr 11 '12 at 19:23
  • You can't read a "local" text file with jQuery. jQuery has no access to the client-side filesystem. Do you mean "local" as in, part of your domain? – user229044 Apr 11 '12 at 19:25
  • 1
    it's not a duplicate 'cause that question is wrongly worded, it's about html files – Gavriel Apr 11 '12 at 19:27
  • @meagar: Yes, I mean local in respect to the remote location of the script's file. – Gadi A Apr 11 '12 at 19:28
  • I'm sorry if this is a duplicate but I couldn't find a question dealing with txt files, as opposed to html files. The problem with "get it as html" is that I want get() to return a string and not something more complicated, and I thought it's only a matter of specifying a correct datatype in order to do this the "right" way. – Gadi A Apr 11 '12 at 19:30
  • As I mentioned, this was not a duplicate since it dealt with reading text files, not html files. – Gadi A Apr 16 '12 at 05:29

2 Answers2

52

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.

Community
  • 1
  • 1
  • Thanks. For future reference: how could I know that "text" is a legitimate datatype to pass? I didn't see in in the jQuery documentation. – Gadi A Apr 11 '12 at 19:31
  • 2
    @GadiA: I just updated my answer with the docs from `$.ajax`. Methods like `$.get` are just a wrapper for `$.ajax`, so you should generally refer to those docs. –  Apr 11 '12 at 19:32
  • Is it possible to read the text file from html `file` upload control? – Manikandan Sethuraju Apr 23 '13 at 07:18
  • This code is generating the below error from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. – Diego Quirós Mar 15 '19 at 16:50
1

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

Gavriel
  • 18,880
  • 12
  • 68
  • 105