Ajax
$ajax{
type:'post',//有get和post
url:'',//接口
date:{},//传输给后台的数据
async:true,//开启异步传输,默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项
//设置为 false。注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执
//行。
dataType:'json',//"xml": 返回 XML 文档,可用 jQuery 处理。
// "html": 返回纯文本 HTML 信息;包含的 script 标签会在插入 dom 时执行。
//"script": 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了
// "cache" 参数。注意:在远程请求时(不在同一个域下),所有 POST
// 请求都将转 为 GET 请求。(因为将使用 DOM 的 script标签来加
载)
// "json": 返回 JSON 数据 。
// "jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?"
// jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。
// "text": 返回纯文本字符串
beforeSend: function() {},//在发送请求之前调用,并且传入一个 XMLHttpRequest 作为参数。
success:function(){ },//成功回调函数
error:function(){},//失败回调函数
}
原生jq实现ajax请求
$ajax({
async: true, // 是否异步
contentType : "application/json",
type: 'POST', // 请求方式
url: 'http//weewewe.djedwed', // 请求接口
dataType:'json', // 参数格式
data: { // 参数
id: '123',
name: 'name111'
},
success: function(res){
cosnole.log('操作成功')
},
error:function(err){
alert(err);
}
})
或者:
$ajax({
//url
url: 'http://127.0.0.1:8000/jquery-server',
//请求参数
data: {a:100, b:200},
//请求类型
type: 'GET',
//响应体结果
dataType: 'json',
//请求头信息
headers: {
Content-Type:'application/json',
Authrization:token,
},
//超时时间
timeout: 2000,
//成功的回调
success: function(data){
console.log(data);
},
//失败的回调
error: function(){
console.log('出错啦!!');
},
})