Ajax

Posted Jomini

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ajax相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Javascript代码片段在drupal中不起作用

前端面试题之手写promise

Ajax 片段元标记 - Googlebot 未读取页面内容

执行AJAX返回HTML片段中的JavaScript脚本

javascript AJAX片段

Spring MVC 3.2 Thymeleaf Ajax 片段