jquery ajax post请求实例

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();
    }
原文地址:https://www.cnblogs.com/watermelonban/p/7701133.html