Ajax请求post方式
得到post.php返回的数据
前端代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>POST</title>
</head>
<body>
<script> var xhr = new XMLHttpRequest(); //注意路径 xhr.open("POST", "http://localhost/request/POST.php"); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send("name=lc&id=1"); xhr.onreadystatechange = function () {
if (xhr.status == 200 && xhr.readyState == 4) {
console.log(JSON.parse(xhr.responseText)); } } </script>
</body>
</html>
本地POST.php
<?php
$name = $_POST['name'];
$id = $_POST['id'];
echo json_encode(array(
'name' => $name,
'id' => $id,
));
ok 完事