美文网首页
xhr.send()异常捕获

xhr.send()异常捕获

作者: VIAE | 来源:发表于2020-03-06 14:30 被阅读0次

用的原生XMLHttpRequest,发现xhr.send(data)请求超时以后不会报错
修改了一下代码,设置send请求超时时间,获取xhr.send()异常

new Promise(function(resolve,reject){
    //原生构造ajax请求
    let xhr = new XMLHttpRequest();
    //设置请求超时处理
    xhr.ontimeout = function () {
        console.error(" timed out");
    };
    xhr.responseType = 'json';
    //post请求需要的参数
    let data = new FormData();
    data.append('length', length);
    //post需要设置请求头
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset-UTF-8");
    //请求响应
    xhr.onload = function() {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                resolve(this.response)
            } else {
                console.error(xhr.statusText);
            }
        }
    };
    xhr.open('post','/fa/b');
    //设置请求超时的时间
    xhr.timeout = 2000;
    //发送请求
    xhr.send(data);
}).then((res)=>{
    console.log(res)
},
(err)=>{
    console.log(err);
});

相关文章

  • xhr.send()异常捕获

    用的原生XMLHttpRequest,发现xhr.send(data)请求超时以后不会报错修改了一下代码,设置se...

  • Python面向对象2

    异常 捕获异常image.png根据错误类型捕获异常image.png 未知错误捕获image.png 捕获异常完...

  • 工作总结-002

    异常捕获service捕获业务异常,自定义BaseException;interface捕获业务异常,以及其他异常...

  • C++学习第20课,异常

    1 异常 一句话概括:捕获异常 1.1 谁捕获异常?捕获谁? A捕获B A() { try{ B(); } ...

  • Python 面向对象 - 08 异常

    目录一、概念二、捕获异常2.1 简单的捕获异常语法2.2 错误类型捕获2.3 异常捕获完整语法三、异常的传递四、抛...

  • springboot 异常捕获和处理

    springboot 异常捕获和处理 异常捕获处理

  • iOS 异常捕获处理机制(初级篇)

    一、异常处理简介 二、异常捕获案例 使用@try catch捕获异常 例1是最简单的一种写法: 捕获异常之嵌套捕获...

  • python3.7异常小记

    首先上示例代码: 这段代码包含了,异常分支类型捕获,无异常捕获,有无异常都会捕获的执行逻辑。 捕获全局异常 对入口...

  • Python异常处理

    Python异常处理 一、捕获异常 1.异常捕获语法 2.错误类型捕获 except:处理所有异常。except ...

  • 自定义异常捕获 与 自定返回json 数据格式

    1. 自定义异常捕获 自定的异常 2 捕获异常 =================================...

网友评论

      本文标题:xhr.send()异常捕获

      本文链接:https://www.haomeiwen.com/subject/ykqvrhtx.html