JavaScript:检测 AJAX 请求 [英] JavaScript: Detect AJAX requests

查看:16
本文介绍了JavaScript:检测 AJAX 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用通用 JavaScript(而不是框架)检测网页上的全局 AJAX 调用(特别是响应)?

Is there any way to detect global AJAX calls (particularly responses) on a web page with generic JavaScript (not with frameworks)?

我已经查看了问题JavaScript 检测 AJAX 事件",在 StackOverflow 上,并尝试将接受的答案的代码修补到我的应用程序中,但没有用.我之前也从来没有用 AJAX 做过任何事情,所以我不知道如何修改它才能工作.

I've already reviewed the question "JavaScript detect an AJAX event", here on StackOverflow, and tried patching in the accepted answer's code into my application but it didn't work. I've never done anything with AJAX before either so, I don't know enough to modify it to work.

我不需要任何花哨的东西,我只需要检测所有(实际上是具体的,但我必须先检测所有并从那里开始)AJAX 响应并将它们修补到 IF 语句中以供使用.所以,最终,我想要类似的东西:

I don't need anything fancy, I just need to detect all (specific, actually, but I'd have to detect all first and go from there) AJAX responses and patch them into an IF statement for use. So, eventually, I'd like something like:

if (ajax.response == "certainResponseType"){
    //Code
}

,例如.

更新:看来我应该澄清一下,我不是在尝试发送请求 - 我正在开发一个内容脚本,我需要能够检测网页的 AJAX 请求(不是我自己的请求),所以我可以在检测到响应时执行一个函数.

Update: It seems I should clarify that I'm not trying to send a request - I'm developing a content script and I need to be able to detect the web page's AJAX requests (not make my own), so I can execute a function when a response is detected.

推荐答案

这里有一些代码(通过粘贴到 Chrome 31.0.1650.63 的控制台进行测试)用于捕获和记录或以其他方式处理 ajax 请求及其响应:

Here's some code (tested by pasting into Chrome 31.0.1650.63's console) for catching and logging or otherwise processing ajax requests and their responses:

(function() {
    var proxied = window.XMLHttpRequest.prototype.send;
    window.XMLHttpRequest.prototype.send = function() {
        console.log( arguments );
        //Here is where you can add any code to process the request. 
        //If you want to pass the Ajax request object, pass the 'pointer' below
        var pointer = this
        var intervalId = window.setInterval(function(){
                if(pointer.readyState != 4){
                        return;
                }
                console.log( pointer.responseText );
                //Here is where you can add any code to process the response.
                //If you want to pass the Ajax request object, pass the 'pointer' below
                clearInterval(intervalId);

        }, 1);//I found a delay of 1 to be sufficient, modify it as you need.
        return proxied.apply(this, [].slice.call(arguments));
    };


})();

此代码通过接受的答案解决了上述问题:

This code solves the above issue with the accepted answer:

请注意,如果您使用框架(如 jQuery),它可能不起作用,因为他们可能会在调用发送后覆盖 onreadystatechange(我认为jQuery 确实如此).或者他们可以覆盖发送方法(但这不太可能).所以这是一个部分的解决方案.

Note that it may not work if you use frameworks (like jQuery), because they may override onreadystatechange after calling send (I think jQuery does). Or they can override send method (but this is unlikely). So it is a partial solution.

因为它不依赖于未更改的 'onreadystatechange' 回调,而是监视 'readyState' 本身.

Because it does not rely on the 'onreadystatechange' callback being un-changed, but monitors the 'readyState' itself.

我从这里修改了答案:https://stackoverflow.com/a/7778218/1153227

这篇关于JavaScript:检测 AJAX 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆