Custom ajaxTransport function without specified da

2019-03-28 18:28发布

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! -_-

1条回答
倾城 Initia
2楼-- · 2019-03-28 19:17
$.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

查看更多
登录 后发表回答