fetch的用法ajax,Promise发送Ajax请求、fetch用法

实例方法:

Document

/*

基于Promise发送Ajax请求

*/

function queryData(url) {

var p = new Promise(function(resolve, reject){

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function(){

if(xhr.readyState != 4) return;

if(xhr.readyState == 4 && xhr.status == 200) {

// 处理正常的情况

resolve(xhr.responseText);

}else{

// 处理异常情况

reject('服务器错误');

}

};

xhr.open('get', url);

xhr.send(null);

});

return p;

}

// queryData('http://localhost:3000/data')

// .then(function(data){

// console.log(data);

// },function(info){

// console.log(info)

// });

// ============================

// 发送多个ajax请求并且保证顺序

queryData('http://localhost:3000/data')

.then(function(data){

console.log(data)//data 是上次异步处理返回的结果

return queryData('http://localhost:3000/data1');

})

.then(function(data){

console.log(data);

return queryData('http://localhost:3000/data2');

})

.then(function(data){

console.log(data)

});

复制代码

cf5b02738308666b527115a1a157588a.png

8972d27036352d71a384840e0e495f0c.png

58eaf4bb3c16614f6a70f12e6ce5c87a.png

返回的是普通值传递给then 默认会生成一个new Promise实例对象 实现链式操作

2fa88efc947a9214c55ee7f4124dae74.png

c4b01baf10b77c119d61a6f1b920c6af.png

对象方法:

Document

/*

Promise常用API-对象方法

*/

// console.dir(Promise)

function queryData(url) {

return new Promise(function(resolve, reject){

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function(){

if(xhr.readyState != 4) return;

if(xhr.readyState == 4 && xhr.status == 200) {

// 处理正常的情况

resolve(xhr.responseText);

}else{

// 处理异常情况

reject('服务器错误');

}

};

xhr.open('get', url);

xhr.send(null);

});

}

var p1 = queryData('http://localhost:3000/a1');

var p2 = queryData('http://localhost:3000/a2');

var p3 = queryData('http://localhost:3000/a3');

// Promise.all([p1,p2,p3]).then(function(result){

// console.log(result)

// })

Promise.race([p1,p2,p3]).then(function(result){

console.log(result)

})

复制代码

在方法 中返回return 的是new Promise实例对象

在外面定义变量 接收 Promise实例对象

Promise.all 全部返回的结果都执行

Promise.race 只要有一个结果返回就输出

fetch基本使用

df827dbaa6fb0ef29b066bfe9b804cef.png

f4158284418f68aedda008e944a5c32a.png

官方地址:

c3db19ff6a24f06030efa58d73faa995.png

ecac1ac7c87be769f872d76cb3a1853a.png

4f121311ba5b99439fb5cd1928626c1b.png

/*

Fetch API 基本用法

*/

fetch('http://localhost:3000/fdata').then(function(data){

// text()方法属于fetchAPI的一部分,它返回一个Promise实例对象,用于获取后台返回的数据

return data.text();

}).then(function(data){

console.log(data);

})

复制代码

post请求:

第一种用法:

be00b11346f9486b88d9580f39880df2.png

b3b0221710074947ca619e1d53a8c006.png

第二种:

4dbfa5c8fdd37afd64fd8f8850682e6a.png

92a9030f43f029240a09501afee6f216.png

put也支持上面的post第一种传参的方式

put 和 post 都是类似的

Document

/*

Fetch API 调用接口传递参数

*/

// GET参数传递-传统URL

// fetch('http://localhost:3000/books?id=123', {

// method: 'get'

// })

// .then(function(data){

// return data.text();

// }).then(function(data){

// console.log(data)

// });

// GET参数传递-restful形式的URL

// fetch('http://localhost:3000/books/456', {

// method: 'get'

// })

// .then(function(data){

// return data.text();

// }).then(function(data){

// console.log(data)

// });

// DELETE请求方式参数传递

// fetch('http://localhost:3000/books/789', {

// method: 'delete'

// })

// .then(function(data){

// return data.text();

// }).then(function(data){

// console.log(data)

// });

// POST请求传参

// fetch('http://localhost:3000/books', {

// method: 'post',

// body: 'uname=lisi&pwd=123',

// headers: {

// 'Content-Type': 'application/x-www-form-urlencoded'

// }

// })

// .then(function(data){

// return data.text();

// }).then(function(data){

// console.log(data)

// });

// POST请求传参

// fetch('http://localhost:3000/books', {

// method: 'post',

// body: JSON.stringify({

// uname: '张三',

// pwd: '456'

// }),

// headers: {

// 'Content-Type': 'application/json'

// }

// })

// .then(function(data){

// return data.text();

// }).then(function(data){

// console.log(data)

// });

// PUT请求传参

fetch('http://localhost:3000/books/123', {

method: 'put',

body: JSON.stringify({

uname: '张三',

pwd: '789'

}),

headers: {

'Content-Type': 'application/json'

}

})

.then(function(data){

return data.text();

}).then(function(data){

console.log(data)

});

复制代码

Fetch响应结果的数据格式

ee931f914ed789fd4ccfc967c60dee59.png

dc473395399ee84e207b1936c9c5ab90.png

ab9ab0445265055e6273e5325524544e.png

e2907ee0f406733f370d6b1ba1cef4d5.png

但是他是字符串

c003d09cd61931014b1083029ee8693c.png

我们要拿到它里面的属性

48c69afbb605711ffa5da8339763e7e5.png

a0302484709e318690614531c99aa6c2.png

/*

Fetch响应结果的数据格式

*/

fetch('http://localhost:3000/json').then(function(data){

// return data.json();

return data.text();

}).then(function(data){

// console.log(data.uname)

// console.log(typeof data)

var obj = JSON.parse(data);

console.log(obj.uname,obj.age,obj.gender)

})

复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值