1:在浏览器中找到当前get请求
代码
JS:
let xhr = new XMLHttpRequest();
xhr.open('get', 'get.php');
xhr.send();
PHP:
<?php
echo "hello";
?>
结果
F12>网络(Network),找到get.php点开
在响应(Response)中可以看到PHP文件的内容
2.往指定文件写入新内容
代码
HTML:
<input type="button" onclick="fn()" value="发送">
<script>
function fn() {
//1 创建对象
let xhr = new XMLHttpRequest();
//2 设置请求
xhr.open('get', 'get.php');
// 3 发送请求
xhr.send();
}
</script>
PHP:
<?php
$str="hello world";
$f=fopen('a.txt','a');//'a'代表访问权限是all
fwrite($f,$str);//fwrite(file,string,length)用于写入文件
?>
结果
点击一次按钮,就往a.txt中写入一行helloworld
3.返回值的接收
代码
HTML:
<button