通过jQuery检测Ajax请求 [英] detect ajax request by jquery

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

问题描述

我正在使用asp控件,并且有一些下拉列表,这些下拉列表可以选择返回服务器并执行一些操作(通过asp ajax) 我要寻找的是在ajax调用开始时通过jquery进行检测 我已经尝试了以下呼叫:

I'm working with asp controls and I have some dropdownlist which in selection go back to the server and do some action ( via asp ajax ) what I'm l looking for is detecting via jquery when the ajax call is starting I've tried the following call :

  $.ajaxSetup({
                    beforeSend: function (jqXHR, settings) {
                        alert("ok");
                        return false;
                    }
                });

also  $(document).ajaxStart(function () {
                   alert("OK");
                });

但是这些都不起作用

推荐答案

好吧,如果$(document).ajaxStart(function() {});对您不起作用, 尝试一点原始的js,

well, if $(document).ajaxStart(function() {}); is not working for you, try a bit raw js,

var oldXHR = window.XMLHttpRequest;

function newXHR() {
    var realXHR = new oldXHR();
    realXHR.addEventListener("readystatechange", function() {
        if(realXHR.readyState==1){
            alert('server connection established');
        }
        if(realXHR.readyState==2){
            alert('request received');
        }
        if(realXHR.readyState==3){
            alert('processing request');
        }
        if(realXHR.readyState==4){
            alert('request finished and response is ready');
        }
    }, false);
    return realXHR;
}
window.XMLHttpRequest = newXHR;

它应该为您提供ajax请求的所有状态,并检查哪种状态对您有效,然后您可以删除其余的if条件.您可以将其放置在$(document).ready(function(){});

it should give you all the states of a ajax request and check which one works for you, and then u can remove rest of the if conditions. you can place it outside of $(document).ready(function(){});

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

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