谷歌浏览器上的xmlhttprequest,有时返回net :: connection_reset错误

时间:2016-04-27 05:23:10

标签: javascript ajax google-chrome xmlhttprequest

我已被委任为websocket服务器创建xhr post请求,

但是每当使用chrome时,它会在50%的时间内返回net :: ERR_CONNECTION_RESET。

在safari,firefox和microsoft edge上测试的其他浏览器上不会发生这种情况。

function doPost(url, data) {
    promisePost(url, data)
    .then(
        function (val) {
            console.log("returns: " + val);

        }, function (error) {
            console.log("error: " + error);
        })
};

var promisePost = function (url, data) {
     return new Promise(function (resolve, reject) {
        try {
            var xhr = new XMLHttpRequest();

            xhr.upload.onabort = function () {
                reject("aborted by user");
            };

            xhr.timeout = 5000;

            xhr.ontimeout = function () {
                reject("error@timeout");
            };

            var handleStateChange = function () {

                switch (xhr.readyState) {

                    case 4:
                        //responses
                        if (xhr.status == 0) {
                            //undefined error it return xhrstatus 0
                            reject("error@xhrcode:" + xhr.status);
                        } else {
                            if (xhr.responseText == "") {
                                reject("error@response is empty");
                            } else
                                resolve("done@xhrcode:" + xhr.status + "@responses:" + xhr.responseText);
                        }
                        break;
                    default:
                        //error with statuscode
                        reject("error@xhrcode:" + xhr.status + "@responses:" + xhr.responseText);
                };
            };

            xhr.onerror = function (e) {
                reject("status code: " + xhr.status + " " + e);
            };
            xhr.onreadystatechange = handleStateChange;
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            xhr.send(data);

        } catch (e) {
            //error in script
            reject("scerror " + e.message);
        }})
};

我100%确定服务器已经有cors支持,因为我已经通过其他浏览器测试了它。

任何人都知道如何解决这个问题?过去3周一直被困在这个

0 个答案:

没有答案