简体   繁体   中英

How to trace cors request using javascript

We have third party js that is referenced through cdn. We are using third party service to track user behavior. We have implemented code to call a function from that js that sends requests to a different domain. We want to trace those requests sent to different domain using javascript. How can I do it using javascript?

I have tried below code to trace requests, but it seems it only traces same domain requests.

var open = window.XMLHttpRequest.prototype.open,
    send = window.XMLHttpRequest.prototype.send;

function openReplacement(method, url, async, user, password) {
    this._url = url;
    console.log('url -------- ' + url);
    return open.apply(this, arguments);
}

function sendReplacement(data) {
    debugger;
    console.log('data -------- ' + data);
    return send.apply(this, arguments);
}

window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;

if you using jquery, it have some global ajax function: for example:

    let open = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function () {
        this.addEventListener("load", event => function () {
            console.log(event.target);
            if (event.target.responseURL.indexOf('YourDomain')>-1) {
               //Do Something
            }
        }, false);
        open.apply(this, arguments);
    };

source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM