AJAX 前后端交互 验证信息是否正确

时间:2021-06-16 16:34:44

1、前段:

 function checkPtCode(obj){
$.ajax({
type: "post",
url: "xxxxxxx",
data: {"xx":obj.value},
cache: false,
async : true,
dataType:"json",
type:"POST",
success: function (result){
if(result.flag == "No"){
jAlert("编码库不存在此编码,请修正!");
}
}
});
}

2、后端

 public void checkPtCode(HttpServletRequest request,
HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
try {
response.setContentType("text/html;charset=UTF-8");
response.setContentType("text/html");
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
String ptCode = request.getParameter("ptCode"); if(ptCode != null && ptCode.trim().length() != 0){
// 编码库校验
List<DrpProductType> list = this.controlRelationService.checkCode(ptCode);
if(list != null && list.size() != 0){
json.put("flag", "ok");
out.print(json.toString());
}else {
json.put("flag", "No");
out.write(json.toString());
}
}
} finally {
out.close();
}
}