码迷,mamicode.com
首页 > Web开发 > 详细

ajax结构体请求后台

时间:2015-05-02 18:16:53      阅读:291      评论:0      收藏:0      [点我收藏+]

标签: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;


}


ajax结构体请求后台

标签:ajax

原文地址:http://hatch.blog.51cto.com/9349645/1641291

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!