ajax struts action异步请求返回提示刷新

前端:

//取消关注 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&param=1&userId="+fdUserId+"&followId="+followId+"&currentPage=${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&currentPage=${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;

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值