原生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' => '*']); } }
标签:原生,XMLHttpRequest,cid,text,xhttp,JS,AJAX,var,responseText 来源: https://www.cnblogs.com/xuanjiange/p/12880987.html