关于异步获取的四种方式,以Grails框架为例

来源:互联网 发布:ips显示器知乎 编辑:程序博客网 时间:2024/05/01 20:22
第一种   Get异步获取:
前台:  
function getdata(){     

                $.get("${createLink(action: "getdate")}",{"id":1},function(data){
                     $("#show").html(data)
                 })
            }

后台:
def getdate(){           

       render("hahaha")

 }


第二种   Post方式获取:
前台:
 function getdata1(){   //Post异步获取

                $.post("${createLink(action: "getdate1")}",{"id":1},function(data){
                    $("#show").html("姓名:"+data.name+"<br/>地址:"+data.address+"<br/>性别:"+data.sex+"<br/>年龄:"+data.age)
                },"json")
            }


后台:
def getdate1(){          

        Student student = Student.findById(Integer.parseInt(params.id))
        render student as JSON

    }

第三种  Ajax异步获取:
前台:
function getdata2(){   

                $.ajax({
                    url:"${createLink(action: "getdate2")}",
                    data:{id:2},
                    type:"POST",
                    dataType:"json",
                    success:function(data){
                        $("#show").html("姓名:"+data.name+"<br/>地址:"+data.address+"<br/>性别:"+data.sex+"<br/>年龄:"+data.age)
                    },
                    error:function(er){
                       alert("失败")
                    }
                })
           } 

后台:
 def getdate2(){          

        Student student = Student.findById(Integer.parseInt(params.id))
        render student as JSON

    } 

第四种  getJSON异步获取:
前台:
function getdata3(){      

                   $.getJSON("${createLink(action: "getdate3")}",{id:3},function(data){
                       $("#show").html("姓名:"+data.name+"<br/>地址:"+data.address+"<br/>性别:"+data.sex+"<br/>年龄:"+data.age)
                    })


               }

后台:
def getdate3(){           //ajax  getJSON方式获取

        Student student = Student.findById(Integer.parseInt(params.id))
        render student as JSON

    }
原创粉丝点击