1. 程式人生 > >SSH通過ajax向action中傳值以及ajax非同步重新整理頁面

SSH通過ajax向action中傳值以及ajax非同步重新整理頁面

js程式碼:

function send(aid,key,account,pswd){
	  var status = $("input[id='"+account+"']").val();
	  $.ajax({
	    type:"post",
	    url:"order_sendAccount.action",//需要用來處理ajax請求的action,addCart為處理的方法名
	    data:{//設定資料來源
	      aid:aid,
	      pname:key,
	      account:account,
	      pswd:pswd,
	      status:status,
	    },
	    dataType:"json",//設定需要返回的資料型別
	    success:function(data){
	    	//window.location.reload();
	    	var d = eval("("+data+")");//將資料轉換成json型別,可以把data用alert()輸出出來看看到底是什麼樣的結構
	        //得到的d是一個形如{"key":"value","key1":"value1"}的資料型別,然後取值出來    
	        if(d.result){
	        	alert("傳送成功!");
	        	window.location.reload();
	        }
	    },
	    error:function(){
	      alert("系統異常,請稍後重試!");
	    }//這裡不要加","
	  });
	}

表單程式碼:

                    <s:iterator var="map"  value="accountMap">
						    <s:iterator value="#map.value" var="pass">
							<tr>
								<td><s:property value="#map.key" /></td>
								<td><s:property value="#pass.account" /></td>
								<td><s:property value="#pass.pswd" /></td>
								<!-- 如果賬號未使用 -->
								<s:if test="#pass.status==null||#pass.status==''">
								<td>未使用</td>
								<td><input id="<s:property value="#pass.account" />" type="text" name="email"/></td>
								<td><input id="send" type="button" value="傳送" onclick="send('<s:property value="#pass.aid" />','<s:property value="#map.key" />','<s:property value="#pass.account" />','<s:property value="#pass.pswd" />')" /></td>
								</s:if>
								<!-- 如果賬號已使用 -->
								<s:else>
								<td><s:property value="#pass.status" /></td>
								<td>此賬號已使用</td>
								<td>無法操作</td>
								</s:else>
							</tr>
							</s:iterator>
						</s:iterator>

action程式碼:


public String sendAccount() {
		String pname=request.getParameter("pname");
		String account=request.getParameter("account");
		String pswd=request.getParameter("pswd");
		String status=request.getParameter("status");
		String str=request.getParameter("aid");
		Integer aid=Integer.parseInt(str);
		log.info("輸出測試資料pname:"+pname);
		log.info("輸出測試資料status:"+status);
		log.info("輸出測試資料aid:"+aid);
		log.info("輸出測試資料account:"+account);
		log.info("輸出測試資料pswd:"+pswd);
		Pass pass=passService.findByAid(aid);
		pass.setStatus(status);
		passService.update(pass);
		//User existUser = (User) ServletActionContext.getRequest().getSession().getAttribute("existUser");
		//傳送賬號
		HashMap<String,String> count=new HashMap<String,String>();
		String passkey="使用者名稱為:"+account+",密碼為:"+pswd;
		count.put("測評名稱:"+pname+"  ", passkey);
		MailUitls.sendAccount(status, count);
		HashMap<String, Boolean> map = new HashMap<String, Boolean>();
		map.put("result", true);//需要創一個result物件
		JSONObject json = JSONObject.fromObject(map);
		result = json.toString();
		return SUCCESS;
	}