开发者

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

I'm making an AJAX call to re开发者_开发知识库trieve 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?


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


Are you loading up the page in your browser over HTTP or just opening it as a regular file? Does the address of the page running the javascript begin with http: or file: ? I suspect it is the latter, and Ajax calls work differently in that situation (see response by tvanfonsson below). If you're building a web app that will be served over HTTP later, try running your page using a local HTTP server.


You get different response codes when opening a local file via XMLHttpRequest than you do when using an HTTP request. I suspect that since you are opening a a local file, jQuery is choking on the response code, thinking it's an error because it's not 200 OK.

Reference

Example: Non-HTTP synchronous request

Despite its name, XMLHttpRequest can be used for non-HTTP requests. This example shows how to use it to fetch a file from the local file system.

var req = new XMLHttpRequest();
req.open('GET', 'file:///home/user/file.json', false); 
req.send(null);
if(req.status == 0)
  dump(req.responseText);

The key thing to note here is that the result status is being compared to 0 for success instead of 200. This is because the file and ftp schemes do not use HTTP result codes


Supported types are:

* "xml": Treat the response as an XML document that can be processed via jQuery. 
* "html": Treat the response as HTML (plain text); included script tags are evaluated. 
* "script": Evaluates the response as JavaScript and evaluates it. 
* "json": Evaluates the response as JSON and sends a JavaScript Object to the success callback. 

Perhaps use should use "html" instead of "text" since jQuery seems to not parse anything if you specify html as the type.


jQuery doesn't seem to have this issue on chrome and ie8.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜