weixin_33747129 2011-02-03 11:17 采纳率: 0%
浏览 535

jQuery ajax请求代理

How I can do the following operation with jQuery:

  1. Some library send ajax request via $.ajax
  2. I need to catch all these requests, and in some cases abort them, and instead pass off another data.

I found, that in jQuery 1.5 there were introduced new methods, such as ajaxPrefilter and ajaxTransport. I also tried ajaxSetup with beforeSend, but I can't achieve 2 points of these working...

  • 写回答

3条回答 默认 最新

  • DragonWar% 2011-02-03 12:15
    关注

    Don't use this unless until you are damn sure about what you are doing

    I am not sure about the ajax intercepter libraries. But i can tell you the nasty hack

    • Take copy of your original jquery ajax instance

            var oldAjaxInstance; //some global variable
            oldAjaxInstance = $.ajax;  //in document load
      
    • And assign your intercepert method to the $.ajax pointer

            $.ajax = myAjaxwrapper;
      

    myAjaxwrapper looks womething like this

    function myAjaxwrapper(a) {
         //your logic to change the request data's
         if (you are ok to allow the ajax call) {
             //re Assgin the actual instance of jquery ajax
             $.ajax =oldAjaxInstance;           
             //and call the method
             $.ajax(a);
         }
         //Otherwise it wont be called
    }
    
    • And onsucess of your ajax call reassign your ajax wrapper to jquery ajax

                 oldAjaxInstance = $.ajax;  
                 $.ajax = myAjaxwrapper;
      
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题