CSDN
博客园
简书

Ajax请求post方式

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 完事

posted @ 2020-12-18 21:00  codernmx  阅读(133)  评论(0)    收藏  举报