jxl1996
<script>
    //step1.创建XMLHTTPRequest对象,对于低版本的IE,需要换一个ActiveXObject对象
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else {
        xhr = new ActiveXObject(\'Microsoft.XMLHTTP\');
    }

    //>>step2.使用open方法设置和服务器的交互信息:
    //设置请求的url参数,参数一是请求的类型,参数二是请求的url,参数三指定是否使用异步,默认是true
    xhr.open("post", "", true);
    //post请求一定要添加请求头才行不然会报错
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    //>>step3.发送请求 GET请求不需要参数,POST请求需要把body部分以字符串或者FormData对象传进去。
    xhr.send();

    //>>step4.注册事件 onreadystatechange 状态改变就会调用
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) { // 成功完成
            // 判断响应结果:
            if (xhr.status === 200) {
                // 成功,通过responseText拿到响应的文本:
                console.log(xhr.responseText);
            } else {
                // 失败,根据响应码判断失败原因:
                console.log(xhr.status);
            }
        } else {
            // HTTP请求还在继续...
        }
    }
</script>

 

分类:

技术点:

相关文章:

  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
  • 2021-11-27
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
猜你喜欢
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
  • 2021-11-20
相关资源
相似解决方案