传送门:
-
1. JavaScript 教程 / 浏览器模型 / Location 对象,URL 对象,URLSearchParams 对象
-
2. MDN - 使用 Fetch
-
2. Fetch - 数据交互方式
-
3. 详解fetch的使用方法及如何接收JS传值
<?php
echo $_POST['foo'] + $_POST['bar'];
?>
一、原生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 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 - 数据交互
<!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 对象
<!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>
<!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>
四、其它接口对象的使用
<!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>