调试Ajax请求Ajax

2023-09-11 00:36:22 作者:不将就不挽留

很多时候,我们面临的问题,当工作在异步JavaScript。我们不知道什么请求被提交并获得什么阶跃响应。

Many time we face issues while working on Asynchronous javascript. We are not sure what request is posted and what respones is obtained.

如何调试Ajax请求。有没有一种简单的方法来做到这一点。

How to Debug AJAX Requests. Is there a easy way to do that.

推荐答案

我是pretty的肯定,如果你的实例化的每个调用一个新的对象的,你可以跟踪每个请求:

I'm pretty sure you can track each request if you instanciate a new object for each call:

var oXhr;

    oXhr = new XMLHttpRequest();
    oXhr.id = (new Date()).getTime(); /* just an example. It might be flawed if you process requests more than once per ms */

    oXhr.onreadystatechange = function() {
        if (oXhr.readyState == 4 && (oXhr.status == 200)) {
            //do your stuff here
            console.log(this.id);
        }
    }