这篇文章为自我总结,恕不细讲。
前台jQuery部分
function useAjax(_type,_url,_dataType,_data,_async)
{
$.ajax(
{
type : _type,
url :_url,
dataType:_dataType,
data:_data,
async:_async,//此处是为了同步,默认为异步
beforeSend : function(XMLHttpRequest)
{
ajaxBeforeSend(XMLHttpRequest);
},
success : function(msg)
{
ajaxSuccess(msg); //成功之后做什么
},
complete : function(XMLHttpRequest, textStatus)
{
},
error : function()
{
alert('服务器连接失败');
}
}
);
}
var userName=$("#userName").val();
var password=$("#password").val();
useAjax("POST","/file/loginAjax","html","userName=" + userName + "&password=" + password,false);
//这里学一下传多个参数
后台部分:
public void loginAjax()throws IOException{
HttpServletResponse response;
HttpServletRequest request;
HttpSession session;
response = ServletActionContext.getResponse();
request =ServletActionContext.getRequest();
user.setUserName(request.getParameter("userName"));
//md5效验尝试了一下后台的
user.setPassword(CommonMethod.md5Encryption(request.getParameter("password")));
response.setContentType("text/html;charset=gb2312");
Boolean flag=userBiz.isUserExist(user);
//这个也行
//response.getWriter().write(flag.toString());
//传给前台的值,这样的话 此函数就可以 不用string了
PrintWriter out = response.getWriter();
out.print(flag.toString());
out.flush();
}
struts.xml部分:
<!-- class="userAction" 是因为我是用了spring method="loginAjax" 就是我指向的方法-->
<action name="loginAjax" class="userAction" method="loginAjax">
<result></result> <!--就感觉等于没写-->
</action>
就这样我实现了,可以参照另一篇传JSON 的。