Ajax原生方法:javascript
<script type="text/javascript"> function delStudent(studentid){ $.ajax({ url:"/project/studentRpc/"+studentid+"/deleteStudentById.json", type:"get", dataType: 'json', success:function(data){ var isDeleted= data.content.successed; alert(typeof isDeleted); if(isDeleted==true){ alert("刪除成功"); window.location.reload(); } } }); } </script>
dataType:'json' 設置返回值類型html
contentType:"application/x-www-form-urlencoded"(默認值)java
contentType參考文章:ajax
http://blog.csdn.net/mhmyqn/article/details/25561535
json
頁面採用回調函數function(data) 處理後臺返回的結果app
a標籤onclick事件觸發ide
<a href ="javascript:void(0);" class="btn btn-default" id ="add" onclick = "return addproduct(${s.id});">加入秒殺</a>
前臺函數
function addproduct(id){ var mprice=document.getElementById("mprice_"+id).value; var number=document.getElementById("number_"+id).value; var sid=document.getElementById("special.id").value; if (mprice==""){ alert("請輸入特價價格"); return false; }else if (number==""){ alert("請輸入特價數量 "); return false; }else { //重點在這兒 $.get("${ctx}/special/addProduct.action?specialVo.quantity="+number+"&specialVo.memberPrice="+mprice+"&specialVo.id="+id+"&special.id="+sid, function(data){ if(data=="true"){ alert("添加成功"); window.location.reload(); } }) /* window.location.href="${ctx}/special/addProduct.action?specialVo.quantity="+number+"&specialVo.memberPrice="+mprice+"&specialVo.id="+id+"&special.id="+sid; */ } }
後臺url
public void addProduct(){ PrintWriter out=null; try { System.out.println(specialVo.getQuantity()); System.out.println(specialVo.getMemberPrice()); System.out.println(specialVo.getId()); System.out.println(special.getId()); HttpServletResponse response=ServletActionContext.getResponse(); out=response.getWriter(); out.print(true); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); out.flush(); out.close(); out.println(0); } }
struts配置action無需resultspa
<action name="addProduct" class="specialAction" method="addProduct" > </action>
方法有兩種,一是返回無類型,即void類型,二是返回Action.NONE(String類型)當是這兩種類型的時候,struts2就不會對result進行主動處理了
即咱們只須要在action方法中,處理ajax調用,而返回void或者"none"就好了
參考文章:
http://bbs.csdn.net/topics/390470284