首页 > Web开发 > 详细

Ajax

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!