Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...