Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does jQuery insist my plain text is not "well-formed"?

Tags:

jquery

ajax

I'm making an AJAX call to retrieve some plain text:

$.ajax({
  url:         "programData.txt",
  type:        "GET",
  dataType:    "text",
  cache:       false,
  success:     processData
});

When I make the request, though, I get the following error:

Error: not well-formed Source File: file:///projects/foo/programData.txt?_=1259694590361 Line: 1, Column: 2

Why is jQuery trying to process my plain text and how do I get it to stop?

like image 854
James A. Rosen Avatar asked Dec 01 '09 19:12

James A. Rosen


1 Answers

Firefox is trying to parse the file as HTML before it even hands it back to jQuery.

There are several reasons why it could be trying to do this. If, as Jaanus suggested, you are using a file:// or chrome:// URL then it doesn't have a MIME type and it assumes HTML. Or your HTTP server could be returning the wrong MIME type.

Starting in jQuery 1.5.1 there is a mimeType option to override the returned MIME type that Firefox sees. So you can do the following:

$.ajax({
  mimeType: 'text/plain; charset=x-user-defined',
  url:         "programData.txt",
  type:        "GET",
  dataType:    "text",
  cache:       false,
  success:     processData
});

Doc on mimeType option is at http://api.jquery.com/jQuery.ajax/

And here is some background on what is going on at the Firefox level: https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Receiving_binary_data

like image 147
studgeek Avatar answered Sep 22 '22 04:09

studgeek