javascript 我不断收到此错误,XML 解析错误:语法错误,但网站仍能正常运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51000009/
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
I keep getting this error, XML Parsing error: syntax error but still the website runs fine
提问by Robbie
I am new to developing websites. I know that I have to map servlets in the web.xml file. The web.xml file is this
我是开发网站的新手。我知道我必须在 web.xml 文件中映射 servlet。web.xml 文件是这个
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>TestApp</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Serve</servlet-name>
<servlet-class>Serve</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Serve</servlet-name>
<url-pattern>/TestApp</url-pattern>
</servlet-mapping>
</web-app>
But when I call the jquery $.ajax() function, I get this error.
但是当我调用 jquery $.ajax() 函数时,我收到了这个错误。
XML Parsing Error: syntax error Location: http://localhost:8080/TestApp/ServeLine Number 1, Column 1:
XML 解析错误:语法错误位置:http://localhost:8080/TestApp/ServeLine Number 1, Column 1:
The AJAX Call is
AJAX 调用是
$.ajax({
url: "Serve",
type: "POST",
success: function(out){
alert(out);
},
error: function(){
alert("No");
}
});
The problem is the servlet still runs fine. The alert(out) works as expected. Please explain why the browser is showing the error and please tell me a solution.
问题是 servlet 仍然运行良好。警报(输出)按预期工作。请解释浏览器显示错误的原因,并告诉我解决方案。
If this is a duplicate question please give a link to the original question.
如果这是一个重复的问题,请提供原始问题的链接。
回答by Cholowao
I recently encountered the same issue. jQuery appeared to be handling the data and the dataType correctly, but instead it was Firefox returning the syntax error, which explains why your code was executing as intended but still printing an error to the console.
我最近遇到了同样的问题。jQuery 似乎正在正确处理数据和数据类型,但实际上是 Firefox 返回了语法错误,这解释了为什么您的代码按预期执行但仍然向控制台打印错误。
If you look in the developer console, you can see that Firefox is interpreting the plain text data as another format (likely XML). Firefox tires to parse the data as XML, but can't because it's not valid XML which results in "Syntax error" being printed to the console.
如果您查看开发者控制台,您可以看到 Firefox 将纯文本数据解释为另一种格式(可能是 XML)。Firefox 厌倦了将数据解析为 XML,但不能,因为它不是有效的 XML,导致“语法错误”被打印到控制台。
Fixing this problem for me involved editing the server so it returned the following header:
为我解决这个问题涉及编辑服务器,因此它返回以下标题:
Content-Type: "text/plain"
Content-Type: "text/plain"
This only appeared to be an issue with Firefox, Chrome did not encounter this issue. There is a Firefox bug here which seems to touch on the issue.
这似乎只是 Firefox 的问题,Chrome 没有遇到此问题。这里有一个 Firefox 错误似乎涉及到这个问题。
回答by tglas
The problem still exists in Firefox 70, at least when requesting a file from the file system. No jquery needed, the behavior can be reproduced with a plain XMLHttpRequest. Calling its overrideMimeTypemethod before sendsolved it for me. Looks like a quite clean solution to me. Example:
该问题在 Firefox 70 中仍然存在,至少在从文件系统请求文件时是这样。不需要 jquery,可以用普通的XMLHttpRequest. overrideMimeType在send为我解决之前调用它的方法。对我来说看起来是一个非常干净的解决方案。例子:
var xhr = new XMLHttpRequest();
xhr.open("GET", window.location, true);
xhr.overrideMimeType("text/html");
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4) alert(xhr.responseText);
}
xhr.send();

