1.jq 中的 ajax
$(document).ready(function(){ $("#result").bind("input propertychange",function(){ $.ajax({ url:"baiduServlet", //服务器地址 async:true, //用同步请求,默认为true异步请求 cache:true, //缓存数据 data:{result:$("#result").val()}, //传参 dataType:"json", //返回数据类型 type: "post", success: function(data,textStatus,jqXHR){ alert(data,name[1]); } }) $.post("baiduServlet",{result:$("#result").val()},function(data)){ alert(data); },"json"); ajaxRequest(); }); })
① 绑定事件的 ajax
$("#username").bind("blur",function(){ var url = "${pageContext.request.contextPath}/ajax/ajaxAction.action"; $.post(url,$("#username").serialize(),function(rst){ $("#aa").html(rst); },"html"); })
② 方法的 ajax
function searchEmpRole(){ $.ajax({ type:"post", url:"${pageContext.request.contextPath}/addEmpRoleList/addEmpRoleListAction.action", dataType:"json", success:function(a){ $.each(a.list,function(i,val){ $("#roleId").append("<option value=" "+val.id+" " id="+val.id+">"+val.roname+"</option>"); }) } }) }
2.Struts2中 ajax 的配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="ajax" namespace="/ajax" extends="json-default"> <action name="ajaxAction" method="execute" class="com.seecen.action.AjaxAction"> <result type="json" name="success"> <param name="includeProperties">list.*</param> </result> </action> </package> </struts>