我从javascript
文件发送ajax post请求但是没有到达servlet
我从IE控制台收到错误,因为找不到error 404
web.xml:
<servlet-mapping>
<servlet-name>Edit</servlet-name>
<url-pattern>/Edit</url-pattern>
</servlet-mapping>
JavaScript AJAX请求:
$.ajax({
type: "post",
url: 'Edit?action=edit',
data: {
env: env,
region: region,
url: url,
rnode: rnode,
user: user,
password: password,
envid: envid,
attuid: attuid
},
success: function() {
alert("row has has been modified");
},
error: function(xhr) {
if (xhr.responseText == "Internal server error")
alert("row cannot be changed , internal server error");
}
});
servlet包和代码:
`/EnvInfo/src/com/amdocs/infraportal/servlets/Edit.java`
public void doPost(HttpServletRequest req , HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html;charset=UTF-8");
PrintWriter pw = res.getWriter();
String n=req.getParameter("action");
System.out.println("welcome"+n);
if(n.equalsIgnoreCase("edit"))
{
JOptionPane.showMessageDialog(null, "request is going to servlet");
}
}
请求根本不会进入servlet,尝试URL
中的所有路径组合但仍无法正常工作
ps - 我从onclick
事件
答案 0 :(得分:0)
理论上这应该有效(我现在无法访问tomcat服务器)
的xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app> <!-- I didn't include all the xml web-app stuff. make sure you do -->
<servlet>
<servlet-name>Edit</servlet-name>
<servlet-class>com.amdocs.infraportal.servlets.Edit</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Edit</servlet-name>
<url-pattern>/Edit</url-pattern>
</servlet-mapping>
</web-app>
爪哇:
public class Edit extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//do stuff
}
}
的Ajax:
$.ajax({
url: 'Edit', //I don't think you should use a query string, you are posting so just add action to your data object
data: yourData,
type: 'post'
}).done(function (data){ //this style of ajax is up to preference
//do something
}).fail(function (err){
//handle error
});