使用 jQuery 捕获 XHR?

发布于 2024-12-15 10:21:25 字数 87 浏览 3 评论 0原文

我将 PhantomJS 与 jQuery 结合使用,我想知道是否可以在 XMLHttpRequest 传递到浏览器时捕获它,而无需自己启动 POST/GET。

I'm using PhantomJS with jQuery and I'm wondering if it's possible to capture an XMLHttpRequest as it's passed to the browser, without initiating the POST/GET yourself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所有深爱都是秘密 2024-12-22 10:21:25

有一些简单的 jQuery 函数可以让您在发送 AJAX 请求之前对其进行修改,但它们仅针对由 jQuery 发起的 AJAX 请求。要捕获页面上的所有 AJAX 请求,请使用以下基本的纯 JS 方法:

(function(open){
    XMLHttpRequest.prototype.open = function(method, url, async, username, password) {
        // modify the variables here as needed or use 'this` keyword to change the XHR object or add event listeners to it
        open.call(this, method, url, async, username, password);
    };
})(XMLHttpRequest.prototype.open);

您还可以重写 XHR send() 函数:

(function(send){
    XMLHttpRequest.prototype.send = function(body) {
       // your code
       send.call(this, body);
    };
})(XMLHttpRequest.prototype.send);

基本上,此代码需要是页面上运行的第一个代码。所有后续的 XHR 请求都将通过重写的函数,允许您更改任何参数等。

注意:如果您希望针对某些版本的 IE,则需要为 IE 用于 AJAX 的 ActiveXObject 实现类似的代码。

There are simple jQuery functions that will let you modify AJAX requests before they are sent, but they only target AJAX requests initiated by jQuery. To catch all AJAX requests on a page, use the following basic JS-only way:

(function(open){
    XMLHttpRequest.prototype.open = function(method, url, async, username, password) {
        // modify the variables here as needed or use 'this` keyword to change the XHR object or add event listeners to it
        open.call(this, method, url, async, username, password);
    };
})(XMLHttpRequest.prototype.open);

You could also rewrite the XHR send() function:

(function(send){
    XMLHttpRequest.prototype.send = function(body) {
       // your code
       send.call(this, body);
    };
})(XMLHttpRequest.prototype.send);

Basically, this code needs to be the FIRST code that runs on your page. All subsequent XHR requests will go through the rewritten function(s), allowing you to change any parameters, etc.

Note: if you wish to target some versions of IE, you need to implement similar code for the ActiveXObject that IE uses for AJAX.

oО清风挽发oО 2024-12-22 10:21:25

ajaxPrefilter 听起来不错,但从未使用过它。这会在发送到服务器之前过滤请求。然后,当请求返回时,您可以使用 pipe 修改您想要的内容:

$.ajaxPrefilter(function(options, originalOptions, jqXHR){
    jqXHR.pipe(function(data){
        //modify the data the way you want
    })
});

ajaxPrefilter sounds good, but never used it. This filters the request before going to the server. You can then use pipe to modify, what you want, when the request returns:

$.ajaxPrefilter(function(options, originalOptions, jqXHR){
    jqXHR.pipe(function(data){
        //modify the data the way you want
    })
});
薄荷→糖丶微凉 2024-12-22 10:21:25

对于页面发出的所有 XHR/AJAX 请求,都会调用 Phantom 的 onResourceReceived 回调。实际上两次,一次使用 stage="start",一次使用 stage="end"。它不会为您提供内容,但会捕获 URL。

Phantom's onResourceReceived callback is invoked for all XHR/AJAX requests made by the page. Twice, actually, once with stage="start", once with stage="end". It won't give you the content, but it will capture the URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文