简体   繁体   English

如何使用 javascript 跟踪 cors 请求

[英]How to trace cors request using javascript

We have third party js that is referenced through cdn.我们有通过cdn引用的第三方js 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.我们已经实现了从向不同域发送请求的 js 调用 function 的代码。 We want to trace those requests sent to different domain using javascript.我们想使用 javascript 跟踪那些发送到不同域的请求。 How can I do it using javascript?如何使用 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:如果您使用 jquery,它有一些全局 ajax function:例如:

    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 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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