微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

原生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' => '*']);
  }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐