【发布时间】:2016-09-21 10:30:41
【问题描述】:
*JQuery 代码:如何使用 ajax 从 HTML 页面调用 servlet 类,我在下面编写了一些代码来调用具有相同 java 包的 servlet 类,我面临 404 page not found 错误
//checking page to be ready for event
$(document).ready(function()
{
//runs after event occured on first dropbox
$('#inpmfudorgId').change(function()
{
var selectedValue = $(this).val();
$.ajax({
//servlet file name to which i want to connect
url: "FileConnection",
//throw alert if connection established
success: function(result)
{
alert("called");
},
//alert if it couldnt make connection with servlet
type: "POST",
error: function(xhr)
{
alert("An error occured: " + xhr.status + " " + xhr.statusText);
}
});
});
});
*Web.xml :我在 web.xml 中定义了实际路径,如下所示
<servlet>
//Name of File
<servlet-name>FileConnection</servlet-name>
//Servlet File Path
<servlet-class>in.mbs.fileupload.ad_forms.FileConnection</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileConnection</servlet-name>
<url-pattern>/FileConnection</url-pattern>
</servlet-mapping>
【问题讨论】:
-
404 表示您调用的 URL 错误或无法识别。您需要检查您的服务器配置
-
尝试从网络浏览器直接访问此 url 以确认此 url 是否可以访问。 404 错误意味着,它无法访问该 url。
-
您的 jQuery 代码不知道 FileConnection 的含义。如果您的应用程序在本地运行,它将有一个类似于
http://localhost:<your_port>/FileConnection的 url 尝试使 AJAX url 指向您的 servlet 的 http url。
标签: javascript java jquery ajax servlets