如果在某些情况下我也设置了标题,如何拦截http请求

时间:2016-10-10 11:47:55

标签: javascript ajax http xmlhttprequest

我使用以下代码拦截所有ajax请求:

(function (open) {
        XMLHttpRequest.prototype.open = function (method, url, async, user, pass) {
            var shdOrNt=onlyUrls?regDta.test(""+url.toLowerCase()):(!regDta.test(""+url.toLowerCase()));
            if(shdOrNt) {
                if(""+url.indexOf(usrDtaUrl)!=-1){
                    localStorage.setItem("usrDta",JSON.stringify(this.responseText))
                }else {
                    this.addEventListener("readystatechange", function () {
                        if (this.readyState == 4) {
                            var response = this.responseText;
                            var absurl=url.split("?")[0];
                            jQuery.ajax({
                                url: "http://localhost:8082/akhilLog/getHttpLog",
                                type: "PUT",
                                data: {
                                    "location": {
                                        "origin": location.origin,
                                        "pathname": location.pathname,
                                        "hash": location.hash
                                    },
                                    "myIpJson": myIpJson,
                                    "clientTime": new Date(),
                                    "data": {
                                        method, url, async, user, pass, response, absurl
                                    }
                                },
                                success: function (result, err) {
                                    if (err) {
                                        console.log(err);
                                    }
                                }
                            });
                        }
                    }, false);
                }
                open.call(this, method, url, async, user, pass);
            }
        };

    })(XMLHttpRequest.prototype.open);

它工作正常但在我的代码的某些部分我(和一些库)在调用open后设置请求的标题

因此它会抛出以下错误:

vendor.min.js:38 Error: Failed to execute 'setRequestHeader' on 'XMLHttpRequest': The object's state must be OPENED.
    at Error (native)
    at http://localhost:5000/static/requireStr/index/vendor.min.js:38:1203
    at q (http://localhost:5000/static/requireStr/index/vendor.min.js:36:17012)
    at http://localhost:5000/static/requireStr/index/vendor.min.js:38:1177
    at n (http://localhost:5000/static/requireStr/index/vendor.min.js:37:31452)
    at http://localhost:5000/static/requireStr/index/vendor.min.js:37:30145
    at http://localhost:5000/static/requireStr/index/vendor.min.js:38:15597
    at m.$eval (http://localhost:5000/static/requireStr/index/vendor.min.js:38:23059)
    at m.$digest (http://localhost:5000/static/requireStr/index/vendor.min.js:38:21607)
    at m.$apply (http://localhost:5000/static/requireStr/index/vendor.min.js:38:23353) undefined(anonymous function) @ vendor.min.js:38(anonymous function) @ vendor.min.js:37(anonymous function) @ vendor.min.js:1(anonymous function) @ vendor.min.js:38$eval @ vendor.min.js:38$digest @ vendor.min.js:38$apply @ vendor.min.js:38l @ vendor.min.js:37J @ vendor.min.js:38t.onload @ vendor.min.js:38
vendor.min.js:38 TypeError: Cannot read property 'url' of undefined
    at responseError (http://localhost:5000/static/requireStr/index/app.min.js:1:30240)
    at http://localhost:5000/static/requireStr/index/vendor.min.js:38:15597
    at m.$eval (http://localhost:5000/static/requireStr/index/vendor.min.js:38:23059)
    at m.$digest (http://localhost:5000/static/requireStr/index/vendor.min.js:38:21607)
    at m.$apply (http://localhost:5000/static/requireStr/index/vendor.min.js:38:23353)
    at l (http://localhost:5000/static/requireStr/index/vendor.min.js:37:30709)
    at J (http://localhost:5000/static/requireStr/index/vendor.min.js:38:915)
    at XMLHttpRequest.t.onload (http://localhost:5000/static/requireStr/index/vendor.min.js:38:1389) undefined(anonymous function) @ vendor.min.js:38(anonymous function) @ vendor.min.js:37(anonymous function) @ vendor.min.js:1(anonymous function) @ vendor.min.js:38$eval @ vendor.min.js:38$digest @ vendor.min.js:38$apply @ vendor.min.js:38l @ vendor.min.js:37J @ vendor.min.js:38t.onload @ vendor.min.js:38

所以我对如何解决这个问题感到困惑?

0 个答案:

没有答案