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.Otherwise jQuery guesses at what was returned.
Remeber,
$.getis just a convenience wrapper for$.ajax. The datatypes are listed in the$.ajax()docs…