2016-04-25 93 views
0

我使用javascript來執行取消或發送操作。當我點擊取消鏈接時,這個頁面沒有變化,我需要刷新整個頁面才能看到更改的狀態。點擊船舶鏈接時,甚至不會刷新整個頁面。我的jsp和控制器代碼顯示如下:爲什麼ajax沒有部分刷新我的頁面?

JSP:

<script> 
    var xmlHttp; 
    xmlHttp = GetXmlHttpObject(); 
    function GetXmlHttpObject() 
    { 
     var xmlHttp = null; 
     try 
     { 
      // Firefox, Opera 8.0+, Safari 
      xmlHttp = new XMLHttpRequest(); 
     } catch (e) 
     { 
      // Internet Explorer 
      try 
      { 
       xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) 
      { 
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
     } 
     return xmlHttp; 
    } 
    function cancelOrder(orderId, rowID){ 
     if (xmlHttp == null) 
     { 
      alert("Your browser does not support AJAX!"); 
      return; 
     } 
     var query = "action=cancel&order=" + orderId; 

     /* alert(orderId); */ 
     xmlHttp.onreadystatechange = function stateChanged() 
     { 
      if (xmlHttp.readyState == 4) 
      { 
       var status = document.getElementById(rowID); 
       alert(status.innerHTML); 
       status.innerHTML = "Canceled"; 
      } 
     }; 
     xmlHttp.open("GET", "manageUserAccount.htm?"+query, true); 
     /* xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); */ 
     xmlHttp.send(null); 
     return false; 
     /* var row = document.getElementById(rowID); 
     row.parentNode.removeChild(row); */ 
    } 
function shipOrder(orderId, rowID){ 
     if (xmlHttp == null) 
     { 
      alert("Your browser does not support AJAX!"); 
      return; 
     } 
     var query = "action=ship&order=" + orderId; 

     /* alert(orderId); */ 
     xmlHttp.onreadystatechange = function stateChanged() 
     { 
      if (xmlHttp.readyState == 4 && xmlhttp.status==200) 
      { 
       var status = document.getElementById(rowID); 
       alert(status.innerHTML); 
       status.innerHTML = "Canceled"; 
      } 
     }; 
     xmlHttp.open("GET", "manageUserAccount.htm?"+query, true); 
     /* xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); */ 
     xmlHttp.send(null); 
     return false; 
     /* var row = document.getElementById(rowID); 
     row.parentNode.removeChild(row); */ 
    } 

</script> 

<c:forEach var="order" items="${user.orders}" varStatus="vs"> 
     <tr> 
       <td>${order.id}</td> 
       <td>${order.createDate}</td> 
       <td>${order.status}</td> 
       <td><a href="manageUserAccount.htm?action=viewDetails&orderId=${order.id}">View Details</a></td> 
       <td><a onclick = "shipOrder(${order.id}, ${vs.index})">Ship the order</a><br><a onclick = "cancelOrder(${order.id}, ${vs.index})">Cancel the order</a></td> 
     </tr> 
</c:forEach> 

控制器:

if(action.equals("cancel")){ 
     OrderDAO orderDao = new OrderDAO(); 
     System.out.println(request.getParameter("order")); 
     Long orderId = Long.parseLong(request.getParameter("order")); 
     Order order = orderDao.get(orderId); 
     order.setStatus("Canceled"); 
     orderDao.save(order); 
     JSONObject obj = new JSONObject(); 
     obj.put("cancel", "Canceled"); 
} 
if(action.equals("ship")){ 
     OrderDAO orderDao = new OrderDAO(); 
     System.out.println(request.getParameter("ship")); 
     Long orderId = Long.parseLong(request.getParameter("ship")); 
     Order order = orderDao.get(orderId); 
     order.setStatus("Shipped"); 
     orderDao.save(order); 
     JSONObject obj = new JSONObject(); 
     obj.put("ship", "Shipped"); 
} 

誰能幫我解決這個問題呢?謝謝!!

回答

0

嗯,檢查我總是使用響應:

if (xmlhttp.readyState==4 && xmlhttp.status==200) 

也許你沒有得到你的迴應權狀態?

+0

嗯試過但仍然沒有改變..>< – user5516371