原生JS请求(AJAX)

页面内容

<!DOCTYPE html>
<html>
<body>

<h1>XMLHttpRequest 对象</h1>

<p id="demo"></p>
 
<script>

  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function(res) {
    if (this.readyState == 4 && this.status == 200) {
    //object
    var text = JSON.parse(this.responseText);
    //string
    //var text = this.responseText;
    console.log(text);
    }
  };
  var cid = 1001;
  xhttp.open("POST", "http://xxx.com", true);
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.send("cid="+cid+"&id=1111&fname=1234");
  
</script>

</body>
</html>

 服务器

<?php

namespace appindexcontroller;
use thinkRequest;
use thinkController;class Index extends Controller
{
    public function index(Request $request)
    {
        
        $response = new Response;
        // ajax 原生JS请求
        $data = $request->param();
        return json($data, 200, ['Access-Control-Allow-Origin' => '*']);
  }
}
原文地址:https://www.cnblogs.com/xuanjiange/p/12880987.html