码迷,mamicode.com
首页 > Web开发 > 详细

Ajax

时间:2018-08-22 16:57:22      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:data   .post   col   try   file   let   row   public   enc   

Ajax 三种方式

get

function fun1(){
     $.get(
            "<%=basePath%>file/ajax",
            {"name":"zhansan","age":25},
            function(data){
                alert(data.name);
            },
            "json"
          
           );
   }
 @RequestMapping(value = "/ajax",method=RequestMethod.GET)
     public @ResponseBody void testajax(HttpServletRequest request, HttpServletResponse response) throws IOException{            
           response.setCharacterEncoding("UTF-8");
          
            try {
                response.getWriter().write("{\"name\":\"完成\"}");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    }

 

post

 function fun2(){
       $.post(
               "<%=basePath%>file/ajax2",
                {"name":"zhansan","age":25},
                function(data){
                    alert(data.name);
                },
                "json"
                
               );
       }
 @RequestMapping(value = "/ajax2",method=RequestMethod.POST)
     public @ResponseBody void testajax2(HttpServletRequest request, HttpServletResponse response) throws IOException{
            
           response.setCharacterEncoding("UTF-8");
            
           try {
                response.getWriter().write("{\"name\":\"完成\"}");
                
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
       
    }

 

ajax

 function fun3(){
       $.ajax({
               url:"<%=basePath%>file/ajax2",
               type:"POST",
               async:true,
               data:{"name":"lucy","age":20},
               success:function(data){
                     alert( "Data Saved: " + data.name );
               },

                error:function(){
                    alert("Failed");
                },
                dataType:"json",
                 
             });
       }

 

Ajax

标签:data   .post   col   try   file   let   row   public   enc   

原文地址:https://www.cnblogs.com/Jomini/p/9517584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!