jQuery XML 解析错误:在 FireFox 中格式不正确,但在 Chrome 中很好

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7642202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:06:25  来源:igfitidea点击:

XML Parsing Error: not well-formed in FireFox but good in Chrome

jqueryajaxfirefox

提问by qc999

I using jQuery Ajax like below:

我使用 jQuery Ajax,如下所示:

$.ajax({
url: 'servlet/*****Servlet',
      dataType: "text",
      success: function(data) {
        var subareaCoordsPGs = preprocessCoords(data);
      }
    });

it works good even i did not set the dataType in Chrome, however, it failed in FF with XML parsing error.

即使我没有在 Chrome 中设置数据类型,它也能很好地工作,但是,它在 FF 中因 XML 解析错误而失败。

Response Headersview source
Server Apache-Coyote/1.1
Transfer-Encoding chunked
Date Tue, 04 Oct 2011 00:08:08 GMT
Request Headersview source
Host localhost:8080
User-Agent Mozilla/5.0 (Windows NT 5.2; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept text/plain, /; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,;q=0.7
Connection keep-alive
X-Requested-With XMLHttpRequest
Referer http://localhost:8080/
*/
Cache-Control max-age=0

XML Parsing Error: not well-formed Location: moz-nullprincipal:{2f6a8381-b987-448b-88c2-e89c4e13440b} Line Number 1, Column 4:

[email protected] -33.9353900931769,151.247877472978 -33.9360784582012,151.24...
------^

响应标题查看源
服务器 Apache-Coyote/1.1
Transfer-Encoding 分块
日期 2011 年 10 月 4 日星期二 00:08:08 GMT
请求标题查看源
主机 localhost:8080
User-Agent Mozilla/5.0 (Windows NT 5.2; WOW64; rv:7.0.1 ) Gecko/20100101 Firefox/7.0.1
接受 text/plain, /; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7, ;q=0.7
Connection keep-alive
X-Requested-使用 XMLHttpRequest
Referer http://localhost:8080/
*/
缓存控制 max-age=0

XML 解析错误:格式不正确 位置:moz-nullprincipal:{2f6a8381-b987-448b-88c2-e89c4e13440b} 第 1 行,第 4 列:

[email protected] -33.9353900931769,151.247877472978 -33.9360784582012,151.24...------
^

after searched, i know it is good to set the proper data type, i want it to be parsed just as normal text, but why Intelligent Guessnot works in FF, even i set it's type is "text"explicitly?

搜索后,我知道设置正确的数据类型很好,我希望它像普通文本一样被解析,但是为什么智能猜测在 FF 中不起作用,即使我将它的类型明确设置为“文本”

回答by Boris Zbarsky

Your server is not returning a content-type, so Firefox assumes that since this is _XML_HttpRequest your response might be XML and tries to parse it. When that fails, it stops trying and reports that this wasn't XML after all.

您的服务器没有返回内容类型,因此 Firefox 假定由于这是 _XML_HttpRequest,您的响应可能是 XML 并尝试解析它。当失败时,它会停止尝试并报告这毕竟不是 XML。

Chrome likely does the same but doesn't report anything.

Chrome 可能会执行相同的操作,但不会报告任何内容。

I suggest actually sending a Content-Type header indicating what your data is.

我建议实际上发送一个 Content-Type 标头,指示您的数据是什么。

回答by Fithage

Just Add this code. The problem is the server did not specify the mime type and firefox takes it to be xml. This code will specify what Mime type the xhr response is going to be.

只需添加此代码。问题是服务器没有指定 mime 类型,firefox 将其视为 xml。此代码将指定 xhr 响应将是什么 Mime 类型。

beforeSend: function(xhr){  xhr.overrideMimeType( "text/plain; charset=x-user-defined" );},