我是开发网站的新手。我知道我必须在 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>
但是当我调用 jquery $.ajax() 函数时,我得到了这个错误。
XML 解析错误:语法错误位置: http://localhost:8080/TestApp/Serve 第 1 行,第 1 列:
AJAX 调用是
$.ajax({
url: "Serve",
type: "POST",
success: function(out){
alert(out);
},
error: function(){
alert("No");
}
});
问题是 servlet 仍然运行良好。 alert(out) 按预期工作。请解释浏览器显示错误的原因,并告诉我解决方案。
如果这是一个重复的问题,请提供原始问题的链接。
原文由 Robbie 发布,翻译遵循 CC BY-SA 4.0 许可协议
我最近遇到了同样的问题。 jQuery 似乎正确处理了数据和数据类型,但 Firefox 却返回了语法错误,这解释了为什么您的代码按预期执行但仍向控制台打印错误。
如果查看开发人员控制台,您会发现 Firefox 正在将纯文本数据解释为另一种格式(可能是 XML)。 Firefox 厌倦了将数据解析为 XML,但不能,因为它不是有效的 XML,这会导致“语法错误”被打印到控制台。
为我解决这个问题涉及编辑服务器,因此它返回以下标头:
Content-Type: "text/plain"
这似乎只是 Firefox 的问题,Chrome 没有遇到这个问题。这里有一个 Firefox 错误似乎涉及到这个问题。
资源