SSM框架中,ajax和相應controller的寫法
阿新 • • 發佈:2019-01-06
頁面上:
$.ajax({ //幾個引數需要注意一下 type: "POST",//方法型別 dataType: "json",//預期伺服器返回的資料型別 url: "wolwohr/test/testSave.jsp" ,//url data: { result : r //多資料用,分隔,字串用:name:'honins' }, success: function (JsonResult) { console.log(JsonResult.message);//列印服務端返回的資料(除錯用) if (JsonResult.code == 200) { alert("SUCCESS"); $('#submit').hide(); $('#submit').parent().hide(); $('#thanks').show(); } ; }, error : function() { alert("異常!"); } });
controller裡:
@RequestMapping(value = "/testSave", method = RequestMethod.POST) @ResponseBody public JsonResult test2(@RequestParam String result) { try { System.out.println(result); return new JsonResult(JsonResult.SUCCESSCODE,"成功"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return new JsonResult(JsonResult.FAILURECODE,"失敗"); }
出現結果