如何使ajax失败可见

How to make ajax failures visible?

本文关键字:失败 ajax 何使      更新时间:2023-09-26

我使用一个小库来进行ajax调用。我希望函数在输入不存在的url时抛出错误。

当前,它无声地失败,并简单地返回响应文本的未定义。这使得排除一般故障更加困难。

是否有简单的方法来做到这一点?

/**************************************************************************************************
AJAX
*/
    // specify type/url/callback in passed object
    Pub.ajax = function (config_ajax) {
        var xhr = new win.XMLHttpRequest();
        // get
        if (config_ajax.type === 'get') {
            xhr.open('GET', config_ajax.url, true);
            xhr.onload = function () {
                if (this.status === 200) {
                    config_ajax.callback(xhr.responseText);
                }
            };
            xhr.send(null);
        }
        // post
        if (config_ajax.type === 'post') {
            xhr.open("POST", config_ajax.url, true);
            xhr.setRequestHeader("Content-type",
                    "application/x-www-form-urlencoded");
            xhr.onload = function () {
                if (this.status === 200) {
                    config_ajax.callback(xhr.responseText);
                }
            };
            xhr.send(config_ajax.data);
        }
        // post for form_data
        if (config_ajax.type === 'multi') {
            xhr.open("POST", config_ajax.url, true);
            xhr.onload = function () {
                if (this.status === 200) {
                    config_ajax.callback(xhr.responseText);
                }
            };
            xhr.send(config_ajax.data);
        }
    };

我假设你不能使用jQuery $。Ajax有一个。success和。error。

您可能需要根据调用的内容和服务器的响应方式使其更加健壮。

if (this.status === 200) {
    config_ajax.callback(xhr.responseText);
} else if (console) {
    console.error("ajax call failed"
}

try this

**************************************************************************************************
AJAX
*/
    // specify type/url/callback in passed object
    Pub.ajax = function (config_ajax) {
        var xhr = new win.XMLHttpRequest();
        // get
        if (config_ajax.type === 'get') {
            xhr.open('GET', config_ajax.url, true);
            xhr.onload = function () {
                if (this.status === 200) {
                    config_ajax.callback(xhr.responseText);
                }
            };
            xhr.send(null);
        }
        // post
        if (config_ajax.type === 'post') {
            xhr.open("POST", config_ajax.url, true);
            xhr.setRequestHeader("Content-type",
                    "application/x-www-form-urlencoded");
            xhr.onload = function () {
                if (this.status === 200) {
                    config_ajax.callback(xhr.responseText);
                }
            };
            xhr.send(config_ajax.data);
        }
        // post for form_data
        if (config_ajax.type === 'multi') {
            xhr.open("POST", config_ajax.url, true);
            xhr.onload = function () {
                if (this.status === 200) {
                    config_ajax.callback(xhr.responseText);
                }
            };
            xhr.send(config_ajax.data);
        }
        // when the ready state changes check for success or failure
        xhr.onreadystatechange=function(){
            if (xhr.readyState==4 && xhr.status==200)
            {
                        //Success
                alert('success');
            }else{
                        //Failure
                alert('Failure');
            }
        }
    };