我有一个带有以下代码的servlet
@WebServlet(urlPatterns = "/attachmentUpload.do")
@MultipartConfig
public class AttachmentUploadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (operationName != null && operationName.equalsIgnoreCase("attachToSNOW")) {
JSONArray runbookRows = new JSONArray(request.getParameter("runbook"));
LOGGER.log(Level.INFO, "runbook rows:{0}", runbookRows.toString());
}
}
}
以及像这样从jsp进行ajax调用
$.ajax({
url: "attachmentUpload.do",
method: "post",
data: {"operationName": "attachToSNOW",
"runbook": JSON.stringify(rowArray)
},
success: function (data) {
console.log("ajax called");
},
error: function (msg) {
console.log("Couldn't attach file");
}
});
[ajax调用似乎没有碰到部署了我的应用程序的servlet(tomcat 8),而在连接到Netbeans的本地tomcat中也是如此。
关于问题到底是什么,我一无所知。
现在坚持了几个星期
编辑真正的问题是,当我从父级jsp打开子级jsp时。在子JSP中,我将一些数据转储到临时文件中,之后将其推送到Web服务,其中提到的ajax调用在子jsp中。而且我没有在tomcat stdout中登录(也没有创建临时文件),因此我知道没有到达部分代码。
一旦我将ajax调用更改为下面,事情就开始起作用了:)
$.ajax({
url: "attachmentUpload.do",
method: "post",
**async: false,
cache: false,**
data: {"operationName": "attachToSNOW",
"runbook": JSON.stringify(rowArray)
},
success: function (data) {
window.opener.log("ajax called");
},
error: function (msg) {
console.log("Couldn't attach file");
}
});
异步和缓存似乎已经做到了魔术
添加异步和缓存解决了我的问题
$.ajax({
url: "attachmentUpload.do",
method: "post",
async: false,
cache: false,
data: {"operationName": "attachToSNOW",
"runbook": JSON.stringify(rowArray)
},
success: function (data) {
window.opener.log("ajax called");
},
error: function (msg) {
console.log("Couldn't attach file");
}
});