function test(){ $.ajax({ //提交數據的類型 POST GET type:"POST", //提交的網址 url:"testLogin.aspx", //提交的數據 該參數為屬性值類型的參數
//(和url?Name="sanmao"&Password="sanmapword"一樣)后台若為SpringMVC接受,注明@RequestParam data:{Name:"sanmao",Password:"sanmaoword"}, //返回數據的格式 datatype: "html",//"xml", "html", "script", "json", "jsonp", "text". //在請求之前調用的函數 beforeSend:function(){$("#msg").html("logining");}, //成功返回之后調用的函數 success:function(data){ $("#msg").html(decodeURI(data)); } , //調用執行后調用的函數 complete: function(XMLHttpRequest, textStatus){ alert(XMLHttpRequest.responseText); alert(textStatus); //HideLoading(); }, //調用出錯執行的函數 error: function(){ //請求出錯處理 } }); }
若要提交json格式的參數:
$(function(){ //隱藏警告信息 $(".alert-warning").hide(); $("#dependentName").blur(function(){ $.ajax({ //提交數據的類型 POST GET type:"POST", //提交的網址 url:"${path}/dependentOffice/checkName", //提交的數據 data: JSON.stringify(GetJsonData()),
//參數格式為json contentType: "application/json; charset=utf-8", //返回數據的格式 datatype: "json",//"xml", "html", "script", "json", "jsonp", "text". //成功返回之后調用的函數 success:function(data){ alert(data); }, //調用出錯執行的函數 error: function(){ //請求出錯處理 alert("請求失敗"); } }); });
//json格式的數據源 function GetJsonData() { var json = { "userName": 'Tom', "tel": '10086' }; return json; } });
后台接受需注明@RequestBody,在就需加入jackson的jar
@RequestMapping("/checkName") @ResponseBody public String checkName(@RequestBody User user ) { //Integer id = dependentOfficeService.selectDependentOfficeByName(dependentName); return user.toString(); }