今天第一次用struts2的ajax,碰到两个问题卡了我两个多小时
1.无论是action返回success还是error都只执行了error回调函数
2.执行完回调函数后会重新加载页面,并在地址栏出现我提交的数据
strut.xml如下
<package name="loginpack" extends="json-default" namespace="/">
<action name="loginaction" class="com.mis.action.LoginAction">
<result name="success" type="json"></result>
<result name="error" type="json"></result>
</action>
</package>
ajax语句如下
$.ajax({
async:true,
type:"post",
url:"${pageContext.request.contextPath}/loginaction",
data:{
username:username,
password:password
},
dataType:"json",
success:function(){
$("#loginwarning").html("登录成功!");
alert("成功!");
},
error:function(){
$("#loginwarning").html("用户名或密码错误!");
alert("失败!");
}
});
这是回调函数执行完地址栏的内容