1. 程式人生 > >原生態js ajax回顧

原生態js ajax回顧

用久了js外掛jquery ext ,發現寫原生態js ,突然很陌生了,這邊重新回顧了下



<span style="white-space:pre">		</span>var XMLHttpReq;  
		function seqChange(script_id,seq,orderType){
       		var url = this.rootPath+'/workPlan/workPlanOperateAction.do?action=scriptSeqChange&script_id='+script_id+'&seq='+seq+'&orderType='+orderType+'&task_id='+<%= Conver.convertNull(request.getParameter("task_id")) %>;
			 createXMLHttpRequest();                                //建立XMLHttpRequest物件  
		     XMLHttpReq.open("post", url, true);  
		     XMLHttpReq.onreadystatechange = processResponse; //指定響應函式  
		     XMLHttpReq.send(null);  
		}
		
		function createXMLHttpRequest() {  
	    try {  
	        XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");//IE高版本建立XMLHTTP  
	    }  
	    catch(E) {  
	        try {  
	            XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");//IE低版本建立XMLHTTP  
	        }  
	        catch(E) {  
	            XMLHttpReq = new XMLHttpRequest();//相容非IE瀏覽器,直接建立XMLHTTP物件  
	        }  
	    }  
  
		} 
		
		function processResponse() {  
		    if (XMLHttpReq.readyState == 4) {  
		        if (XMLHttpReq.status == 200) {  
		            var text = XMLHttpReq.responseText;  
					if("success" == text)
					{
						location.reload();  
					}		  
		  
		        }  
    		}  
  
		}

後臺的程式碼也很簡單

   String rtn = "failure";
        if (result)
        {
            rtn = "success";
        }
        
        JsonResp resp = new JsonResp(response);
        resp.sendJson(rtn);