ajax结构体请求后台

前端之家收集整理的这篇文章主要介绍了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("操作提示",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;


}

原文链接:https://www.f2er.com/ajax/163454.html

猜你在找的Ajax相关文章