前端:
//取消关注 function
function qxGzFun(userId,followId){
if(window.XMLHttpRequest){
xmlObj = new XMLHttpRequest();
}else if(window.ActiveXObject){
xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
}else {
return;
}
xmlObj.onreadystatechange = handleResponse;
//我们通过这个对象的OPEN方法向服务发送请求,该函数声明为XMLHttpRequest.open(String method, String URL, boolean asynchronous);
//method是请求的方式,可以为GET和POST URL是你要请求的资源 asynchronous是布尔类型,为true表示交互设置为异步
xmlObj.open("post","${basePath}index.do?method=toMyzoneFsQxgz",true);
//xmlObj.send("");调用send()(参数是空或是null)将会发起一次请求,对于GET方式的请求,两次同样的请求将会得到相同的结果,由于为将交互设置为异步方式,
//因此要为指定一个回调函数:xmlObj.onreadystatechange = callBackFunction;剩下的事就交给回调函数处理了。有一点要注意了,用Servlet或JSP来响应异步请求时,
//要设置reponse的contentType属性指明是XML格式:response.setContentType("text/xml");
xmlObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlObj.send("followId="+followId+"&userId="+userId);
//window.location.href="${basePath}index.do?method=toMyzoneFsQxgz¶m=1&userId="+fdUserId+"&followId="+followId+"¤tPage=${pageDataOne.currentPage}";
}
function handleResponse(){
if (xmlObj.readyState == 4){//xmlObj loaded
if (xmlObj.status == 200){
alert(xmlObj.responseText+"!");
window.location.href="${basePath}index.do?method=toMyzoneFs¤tPage=${pageData.currentPage}";
// confirm(xmlObj.responseText+"!");
// prompt(xmlObj.responseText+"!");
//window.location.reload();
//var followNumDiv = document.getElementById('followNumDiv');
// followNumDiv.innerHTML = "${followNum}";
}
}
}
后台:
public ActionForward toMyzoneFsQxgz(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
String userId = request.getParameter("userId");
String followId = request.getParameter("followId");
//取消关注
String msg = userFollowService.cancelUserRelation(userId, followId);
try {
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/xml;charset=utf-8");
PrintWriter pw = response.getWriter();
pw.print(msg);
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}