温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Jquery ajax异步提交

发布时间:2020-06-21 20:56:33 来源:网络 阅读:725 作者:jethai 栏目:web开发


Ajax原生方法:

<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' 设置返回值类型

contentType:"application/x-www-form-urlencoded"(默认值)

contentType参考文章:

http://blog.csdn.net/mhmyqn/article/details/25561535



页面采用回调函数function(data) 处理后台返回的结果


a标签onclick事件触发

<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; */
  
 }
  
 }


后台

 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无需result

<action name="addProduct" class="specialAction" method="addProduct" > </action>


方法有两种,一是返回无类型,即void类型,二是返回Action.NONE(String类型)当是这两种类型的时候,struts2就不会对result进行主动处理了

即我们只需要在action方法中,处理ajax调用,而返回void或者"none"就行了



参考文章:

http://bbs.csdn.net/topics/390470284

 

http://blog.csdn.net/xuzhuang2008/article/details/6928304

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI