原生js如下:

<script>
    // 创建请求实例对象
    var myAjax = new XMLHttpRequest();
    // 设置请求的url参数,这是做准备工作
    myAjax.open('get', 'https://autumnfish.cn/search?keywords="嘉宾"');
    // 发送请求
    myAjax.send();
    myAjax.onreadystatechange = function () {
      if (myAjax.readyState === 4 && myAjax.status === 200) {
        console.log(JSON.parse(myAjax.responseText));
      }
    }
  </script>

 

封装如下

  <!-- 自己定义一个对后端请求的方法 -->
  <script>
    function request(method, url, data, success) {
      var ajax = new XMLHttpRequest();
      if (method.toLowerCase() == 'get') {
        if (data) {
          // url += '?';
          // url += data;
          url = `${url + '?'}${data}`;
        } else {
        }
        ajax.open(method, url);
        ajax.send();
      } else {//假设除了get 就是 post
      }
      ajax.onreadystatechange = function () {
        if (ajax.readyState === 4 && ajax.status === 200) {
          success(JSON.parse(ajax.responseText));
        }
      }
    }
    <!-- 调用自己定义的函数 -->
    request('get', 'https://autumnfish.cn/search', 'keywords="Stay"', success);
    function success(res) {
      console.log(res);
    }
  </script>