使用Jquery Ajax调用具有多个参数的servlet时,即使调用成功,也无法调用成功或错误。它绑定到提交按钮上。更改后返回成功事件。
以下是我的参考代码,以防有人需要参考。
$(document).ready( function () {
$("#buttonSave").click(function() {
alert('incustsave');
var name = $("#custname").val();
var gstnumber = $("#gstnumber").val();
var custbizname = $("#custbizname").val();
var email = $("#email").val();
var address = $("#address").val();
var street = $("#street").val();
var city = $("#city").val();
var zip = $("#zip").val();
var phone = $("#phone").val();
var country = $("#ctry").val();
var inputArray = [name, gstnumber, custbizname, email, address, street, city, zip, phone, country];
var Success = false;
alert('added_button_and_dt');
$.ajax({
type: "POST",
url: "RegisterCustomerServlet",
data: {'input': inputArray},
dataType: 'json',
success: function (data) {
alert('sucess');
},
error: function (e) {
alert('error');
}
});
});
});
使用Bootstrap3的HTML(按钮参考)
<div class='wrapper text-center'>
<div class="btn-group">
<button type="button" id="buttonSave" class="btn btn-primary">Save</button>
</div>
</div>
Servlet 参考
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HashMap<String,String> map = new HashMap<String,String>();
CustomerDAO custinfo = new CustomerDAO();
Gson gson = new Gson();
CustomerVO vo = new CustomerVO();
String[] myJsonData = request.getParameterValues("input[]");
logger.info("in custregisterjsoninput" + myJsonData[0] + myJsonData[2] + myJsonData[3] + myJsonData[4]);
map.put("custname", myJsonData[0]);
map.put("getsnumber", myJsonData[1]);
map.put("custbizname", myJsonData[2]);
map.put("email", myJsonData[3]);
map.put("address", myJsonData[4]);
map.put("street", myJsonData[5]);
map.put("city", myJsonData[6]);
map.put("pincode", myJsonData[7]);
map.put("mainphone", myJsonData[8]);
map.put("country", myJsonData[9]);
try {
vo = custinfo.saveCustomerInfo(map);
} catch (Exception e) {
logger.info("aftercall"+e.getMessage());
throw new ServletException(e);
}
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(Utility.convertPOJOtoJason(vo));
}