【JavaScript】fetch()函数、URLSearchParams对象

传送门:
1. JavaScript 教程 / 浏览器模型 / Location 对象,URL 对象,URLSearchParams 对象
2. MDN - 使用 Fetch
2. Fetch - 数据交互方式
3. 详解fetch的使用方法及如何接收JS传值

  • server.php
<?php
	echo $_POST['foo'] + $_POST['bar'];
?>

一、原生ajax - 数据交互

  • index.html
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title></title>
</head>
<body>
	<script>
		// 数据
		var foo = 1,
			bar = 2;
		
		var xhr = new XMLHttpRequest();
		xhr.open('POST', 'server.php', true);
		xhr.onreadystatechange = function() {
			if (this.readyState === 4 && this.status === 200) {
				console.log(this.responseText);
			}
		};
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xhr.send('foo=' + foo + '&bar=' + bar);
	</script>
</body>
</html>

二、fetch - 数据交互

  • index.html()
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title></title>
</head>
<body>
	<script>
		// 数据
		var foo = 1,
			bar = 2;
		
		fetch('server.php', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: 'foo=' + foo + '&bar=' + bar
		}).then(function(response) {
			return response.text();
		}).then(function(response) {
			console.log(response);
		});
	</script>
</body>
</html>

三、使用 URLSearchParams 对象

  • ajax
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title></title>
</head>
<body>
	<script>
		// 数据
		var params = new URLSearchParams({foo: 1, bar: 2});
		
		var xhr = new XMLHttpRequest();
		xhr.open('POST', 'server.php', true);
		xhr.onreadystatechange = function() {
			if (this.readyState === 4 && this.status === 200) {
				console.log(this.responseText);
			}
		};
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xhr.send(params);
	</script>
</body>
</html>
  • fetch
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title></title>
</head>
<body>
	<script>
		// 数据
		var params = new URLSearchParams({foo: 1, bar: 2});
		
		fetch('server.php', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			body: params
		}).then(function(res) {
			return res.text();
		}).then(function(res) {
			console.log(res);
		});
	</script>
</body>
</html>

四、其它接口对象的使用

  • index.html
<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title></title>
</head>
<body>
	<script>
		// 查询参数对象
		const params = new URLSearchParams({foo: 1, bar: 2});
		
		// 头信息对象
		const myHeader = new Headers();
		myHeader.append('Content-Type', 'application/x-www-form-urlencoded');
		
		// 请求对象
		const request = new Request('server.php', {
			method: 'POST',
			headers: myHeader,
			body: params
		});
		
		fetch(request).then(res => res.text())
			.then(res => console.log(res));
	</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值