自定义ajaxTransport功能,无需指定的数据类型不点火(所有!) [英] Custom ajaxTransport function without specified dataType is not firing (AT ALL!)

查看:232
本文介绍了自定义ajaxTransport功能,无需指定的数据类型不点火(所有!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图建立定制ajaxTransports的jQuery 的短路在某些情况下一些工作流程我们的产品。在获得这些传输然而,我得不了成功兑现(而我有很多工作定制阿贾克斯prefilters 的)。

测试多个版本的jQuery的:

  • 1.5.2
  • 1.6.4
  • 1.7.2
  • 1.8.0

测试多种浏览器:

  • 在火狐15
  • 在Chrome浏览器21
  • 在iOS 5中webviews

...

他们没有工作。

的jsfiddle测试用例: http://jsfiddle.net/PVYut/

...

如果我添加一个的dataType 来缩小它,然后它工作正常。

的jsfiddle测试用例: http://jsfiddle.net/PVYut/1/

...

难道我只是在做什么都错了?我很高兴能这么说,只要我能得到这个工作! -_-

解决方案

  $。ajaxTransport(+ *,功能(选件,originalOptions,jqXHR,头,completeCallback){
    的console.log(执行ajaxTransport);
    返回 {
        发送:函数(头,completeCallback){
            completeCallback(404,错误,{});
        },
        中止:函数(){
          / *中止code * /
        }
    }
});

$阿贾克斯(?jqTrans =+(+(新的日期())))
    .done(函数(){
        的console.log(错误:不应该是成功的!);
    })
    .fail(函数(){
        的console.log(成功:如果都失败了。);
    });
 

下面是的jsfiddle

I have been trying to setup custom ajaxTransports for jQuery to short-circuit some workflows in certain scenarios for our product. However, I have had zero success in getting these transports to be honored (whereas I have many working custom ajaxPrefilters).

Tested with multiple versions of jQuery:

  • 1.5.2
  • 1.6.4
  • 1.7.2
  • 1.8.0

Tested with multiple browsers:

  • Firefox 15
  • Chrome 21
  • iOS 5 webviews

...

None of them worked.

JsFiddle test case: http://jsfiddle.net/PVYut/

...

If I add a dataType to narrow it down, then it works fine.

JsFiddle test case: http://jsfiddle.net/PVYut/1/

...

Am I just doing something all wrong? I'd be happy to be told so, so long as I can get this working! -_-

解决方案

$.ajaxTransport("+*", function(options, originalOptions, jqXHR, headers, completeCallback   ) {
    console.log("Executing ajaxTransport");
    return {
        send: function( headers, completeCallback ) {
            completeCallback(404, "error", {});
        },
        abort: function() {
          /* abort code */
        }
    }
});

$.ajax("?jqTrans=" + (+(new Date())))
    .done(function() {
        console.log("ERROR: Should not have been successful!");
    })
    .fail(function() {
        console.log("SUCCESS: Should have failed.");  
    });

Here is jsFiddle

这篇关于自定义ajaxTransport功能,无需指定的数据类型不点火(所有!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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