使用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){}
});