Jquery ajax使用详解

使用ajax需要在网页中导入 jquery文件 可以通过cdn获取或者下载源文件到网站目录

 

Get方式:

函数:     $.get(url [,data] [,success] [,dataType])

参数:      url:请求地址

              data:参数(可选)

              success:请求成功回调函数(可选)

              dataType:参数类型(可选)   

 

举例: 

    [只发送请求忽略其他]    

 $.get("127.0.0.1:80/heart")

    [带参数的请求]               

$.get("127.0.0.1:80/heart",{token:"233j923b4d"})

    [需要成功回调]               

$.get("127.0.0.1:80/heart",function(res){
    //todo
})

    [带参数需要成功回调]     

$.get("127.0.0.1:80/heart",{token:"233j923b4d"},function(res){
    //todo
}) 

 

 

Post方式(和Get方式基本相同):

函数:    $.post(url [,data] [,success] [,dataType])

参数:      url:请求地址

              data:参数(可选)

              success:请求成功回调函数(可选)

              dataType:参数类型(可选)

 

举例: 

    [只发送请求忽略其他]    

 $.post("127.0.0.1:80/heart")

    [带参数的请求]               

$.post("127.0.0.1:80/heart",{token:"233j923b4d"})

    [需要成功回调]               

$.post("127.0.0.1:80/heart",function(res){
    //todo
})

    [带参数需要成功回调]     

$.post("127.0.0.1:80/heart",{token:"233j923b4d"},function(res){
    //todo
}) 

 

万能方式:

函数:    $.ajax([settings])  or $.ajax(url [,settings])

参数:    url:地址

            type:方式 "GET"/"POST" ...

            async:是否异步,默认为true

            beforeSend:发送前回调

            complate:请求完成时运行的回调, 在success 和error函数之后

            contentType:发送的数据类型,默认"application/x-www-form-unlencoded"

            data:发送数据

            error:请求失败回调

            success:请求成功回调

            timeout:请求超时时间,毫秒计算

            

举例:   

[等同上面get方式]        

$.ajax({
    url:"127.0.0.1:80/heart",
    data: {token:"233j923b4d"}
    type:"GET"
    success:function(res){}
})

    [等同上面post方式]       

$.ajax({
    url:"127.0.0.1:80/heart",
    data: {token:"233j923b4d"}
    type:"POST"
    success:function(res){}
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值