ajax结构体请求后台

1.少量数据的请求方式,直接在data中罗列出来

$.ajax({

type : "post",

url : "addOrgUnit",//后台action

contentType : ‘application/x-www-form-urlencoded; charset=utf-8‘,

data : "code=" + code + "&deptname=" + deptname + "&typeid="

+ typeid + "&areaname=" + areaname + "&areaid=" + areaid,//传参

success : function(msg) {//成功返回

$.messager.alert("操作提示", msg,"info",function(){

location.href = "orgUnits";

});


},

error : function() {//返回失败

$.messager.alert("操作提示", "连接失败!","error");

}


});

2.大量数据的请求方式,form表单的序列化

{

 $.ajax({

   cache:true,

           type:"post",

           url:"addexpert",

           data:$(‘#addPJZJ_form‘).serialize(),

           async:false,

           error:function(request){

            $.messager.alert("操作提示", "连接失败!","error");

           },

           success:function(msg){               

              

              $.messager.alert("操作提示", msg,"info",function(){

              document.getElementById("addPJZJ_form").reset();//表单重置

                   location.href="houtai/expertUser";

             // refresh();

              });

             // alert("您的专家信息已添加成功,请审核");

             

      }  

       });

}

}

});

3.json数据的请求方式,dataType为json


$.ajax({

type:"post",

url:"toTabexpert",

data:"expertid="+expertid,

dataType : "json",

error:function(){

$.messager.alert("操作提示", "连接失败!","error");

},

 success : function(data) {               

var result = eval(data);//后台返回的json格式的数据

 $("#info").html("<table bgcolor=‘#CCCCC‘ border=‘0‘ cellpadding=‘5‘ cellspacing=‘1‘><tbody font-size=‘12px‘><tr bgcolor=‘#ffffff‘><td width=‘110‘>1、姓  名</td><td align=‘left‘>"+result[0].expertname+"</td>" +

 "<td>"+

 "</tbody></table>");

$(‘#showExpert‘).dialog(‘open‘);

}


});

    后台json格式的数据

@RequestMapping(value = "toTabexpert")

public @ResponseBody String toTabexpert(HttpServletRequest request,

HttpServletResponse response, int expertid) {


String sql = "from TabExpert te where te.expertid=‘" + expertid + "‘";

List<TabExpert> tabExperts = tabExpertSercive

.getTabExpertListByHql(sql);


List<TabExpert> listExpertinfo = new ArrayList<TabExpert>();

for (int i = 0; i < tabExperts.size(); i++) {

TabExpert tabExpert = new TabExpert();

tabExpert.setExpertname(tabExperts.get(i).getExpertname());

listExpertinfo.add(tabExpert);

}

net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray

.fromObject(listExpertinfo);

String jsonExperts = jsonArray.toString();

System.out.println("sfdsf" + jsonArray);


return jsonExperts;


}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。