原生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 app\index\controller;
use think\Request;
use think\Controller;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' => '*']);
  }
}

 

上一篇:SQL:经典面试50题


下一篇:从零开始写代码-AdaBoost算法的python实现