拦截和响应更改XHR

时间:2019-04-21 19:53:52

标签: javascript ajax xmlhttprequest httpresponse

我正在使用此javascript代码来拦截和更改XmlHttpRequest。它可以很好地处理请求,但不适用于响应。我无法更改回复,该怎么办?

let oldXHROpen = window.XMLHttpRequest.prototype.open;
window.XMLHttpRequest.prototype.open = function(method, url, async, user, password) {


       if (url.startsWith(\"{$_baseurl}\")) {
            url = url.replace(\"{$_baseurl}\", \"{$request_url}\");

        } else if (url.startsWith(\"http://\") || url.startsWith(\"https://\")) {
            // do nothing with the url
        } else if (url.startsWith(\"//\")) {
           url = \"https:\" + url;


        } else if (url.startsWith(\"/\")) {
            url = \"{$request_host}\" + url;

        } else if (url.startsWith(\".\")) {
            url = \"{$request_host}\" + url.replace(\"./\", \"/\");

        } else {
            url = \"{$request_url}\" + url;

        }



 this.addEventListener('load', function() {


  //console.log('load: ' + this.responseText);

 });

 return oldXHROpen.apply(this, arguments);
}

我尝试过,但是没有用

 this.addEventListener('load', function() {

    var jsonResponse = JSON.parse(this.responseText);

  jsonResponse.pageUrl = 'TEST'+jsonResponse.pageUrl;
 }

** $ base url和$ request_uri是由php动态放置的,因此它们的格式是

0 个答案:

没有答案