Ajax GET请求方式
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GET</title>
</head>
<body>
<button id="app">点击发送请求</button>
<div id="box"></div>
<script>
var app = document.getElementById("app");
var box = document.getElementById("box");
app.onclick = function () {
var xhr = new XMLHttpRequest();
//这里注意请求路径http://localhost/request/return.php
//这是本地的
xhr.open("GET", "http://localhost/request/GET.php?name=lcl&id=1&password=aa123456");
xhr.send(null);
xhr.onreadystatechange = function () {
if (xhr.status == 200 && xhr.readyState == 4) {
console.log(JSON.parse(xhr.responseText));
box.innerHTML = xhr.responseText;
}
}
}
</script>
</body>
</html>
这里需要GET.php文件------因为请求的url写的本地http://localhost/request/GET.php
php返回数据
GET.php代码:
<?php
$id = $_GET['id'];
$name = $_GET['name'];
$password = $_GET['password'];
echo json_encode(array(
'id' => $id,
'name' => $name,
'password' => $password,
));
OK结束
转载:https://blog.csdn.net/qq_31754591/article/details/111396146
查看评论