我有一个基于 HTTP servlet 的网络应用程序。这是对我的 servlet 的 JS 请求:
$.ajax({
type: "POST",
url: url,
xhrFields: {
withCredentials: false
},
data:{
"action" : action_type,
"fingerprint" : fingerprint,
"user_id" : user_id,
},
success: function(data) {
alert('sdp inviato!');
},
// vvv---- This is the new bit
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error, status = " + textStatus + ", " +
"error thrown: " + errorThrown
);
}
});
这是我的 servlet 代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action= request.getParameter("action");
UtenteModel um= new UtenteModel();
SDPLineModel sdpmodel= new SDPLineModel();
if (action.equals("CheckFingerprint")) {
String fingerprint = (String) request.getParameter("fingerprint");
String user_id = request.getParameter("user_id");
SDPLines sdpline = sdpmodel.findFingerprintNew(fingerprint, user_id);
if (sdpline == null) {
String message = "Fingerprint non valida!";
System.out.println("Fingerprint not found! ");
}
else {
System.out.println("Fingerprint found! ");
}
}
我的问题是:任何人都可以向我展示一些示例来修改我的 servlet 和 js 代码,设置特定的响应数据(对于这两种情况,找到或未找到指纹)以及如何在成功中通过 JS 代码检查其值:function(data )?
如果我非常理解您的问题是您想通过 JS 代码显示从您的 servlet 到客户端的响应!!你可以试试这个:
if (sdpline == null) {
String message = "Fingerprint non valida!";
response.getOutputStream().print(message);
}else {
String message = ""Fingerprint found! ""
response.getOutputStream().print(message);
}
所以现在在您的 AJAX 请求中,如果您的请求成功完成,您将收到该消息。
},
success: function(data) {
alert('message coming from your servlet is '+data);
//here data will contain one of the messages depending on the if statement
},
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句