✍目录总览:
基本特性
语法结构
fetch(url).thne(fn2)
.thne(fn3)
...
.then(fn)
//客户端请求
<body>
<script type="text/javascript">
//Fetch API 基本用法
fetch('http://localhost:3000/fdata').then(function(data){
// text()方法属于fetchAPI的一部分,它返回一个Promise实例对象,用于获取后台返回的数据
return data.text();
}).then(function(data){
console.log(data);
})
</script>
</body>
//服务器响应
app.get('/fdata', (req, res) => {
res.send('Hello Fetch!')
})
常用配置选项:
//客户端请求
<body>
<script type="text/javascript">
//GET参数传递-传统URL
fetch('http://localhost:3000/books?id=123', {
method: 'get'
})
.then(function(data){
return data.text();
}).then(function(data){
console.log(data)
});
</script>
</body>
//服务器响应
app.get('/books', (req, res) => {
res.send('传统的URL传递参数!' + req.query.id)
})
//客户端请求
<body>
<script type="text/javascript">
//GET参数传递 - restful形式的URL
fetch('http://localhost:3000/books/456', {
method: 'get'
})
.then(function (data) {
return data.text();
}).then(function (data) {
console.log(data)
});
</script>
</body>
//服务器响应
app.get('/books/:id', (req, res) => {
res.send('Restful形式的URL传递参数!' + req.params.id)
})
//客户端请求
<body>
<script type="text/javascript">
//DELETE请求方式参数传递
fetch('http://localhost:3000/books/789', {
method: 'delete'
})
.then(function (data) {
return data.text();
}).then(function (data) {
console.log(data)
});
</script>
</body>
//服务器响应
app.delete('/books/:id', (req, res) => {
res.send('DELETE请求传递参数!' + req.params.id)
})
传递的是传统格式的参数
//客户端请求
<body>
<script type="text/javascript">
//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)
});
</script>
</body>
//服务器响应
app.post('/books', (req, res) => {
res.send('POST请求传递参数!' + req.body.uname + '---' + req.body.pwd)
})
传递的是Json格式的参数
//客户端请求
<body>
<script type="text/javascript">
//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)
});
</script>
</body>
//服务器响应
app.post('/books', (req, res) => {
res.send('POST请求传递参数!' + req.body.uname + '---' + req.body.pwd)
})
//客户端请求
<body>
<script type="text/javascript">
//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)
});
</script>
</body>
//服务器响应
app.put('/books/:id', (req, res) => {
res.send('PUT请求传递参数!' + req.params.id + '---' + req.body.uname + '---' + req.body.pwd)
})
//客户端请求
<body>
<script type="text/javascript">
//Fetch响应text数据格式
fetch('http://localhost:3000/json').then(function(data){
return data.text();
}).then(function(data){
var obj = JSON.parse(data);
console.log(obj.uname,obj.age,obj.gender)
})
</script>
</body>
//服务器响应
app.get('/json', (req, res) => {
res.json({
uname: 'lisi',
age: 13,
gender: 'male'
});
})
处理方式(访问到具体属性值):
1.使用JSON.parse()把字符串转化成对象
var obj = JSON.parse(data)
2.使用 obj.属性名 得到属性值
console.log(obj.uname)
//客户端请求
<body>
<script type="text/javascript">
//Fetch响应json数据格式
fetch('http://localhost:3000/json').then(function(data){
return data.json();
}).then(function(data){
console.log(data.uname)
})
</script>
</body>
//服务器响应
app.get('/json', (req, res) => {
res.json({
uname: 'lisi',
age: 13,
gender: 'male'
});
})