0

If you execute this code in 1.7, the alert appears and 1.8 it does not:

$.when(
$.ajax({
    url: "whatever",
    dataType: "json",
    beforeSend: function(jqXHR, settings) {
        return false;
    }
})).done(function(a1) {
    alert("this appears in 1.7 but not in 1.8!");
});​

I was unable to find anything in the jQuery documentation, the 1.8 release notes, or the jQuery bug tracker that mentions this issue. I'm wondering which behavior is correct and whether someone can provide a link to a bug ticket or Github commit.

Here is a fiddle for this issue:

http://jsfiddle.net/zJddg/

Dave L.
  • 9,595
  • 7
  • 43
  • 69

2 Answers2

2

From the jQuery documentation:

Returning false in the beforeSend function will cancel the request.

The current behavior you are experiencing in 1.8 is the expected behavior, if it wasn't canceling the request in 1.7, that would be a bug.

http://api.jquery.com/jQuery.ajax/

I don't see a bug linked in any of the releases that point to fixing this "bug" though.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • Yes I did see that in the doc, but does "canceling" a request mean that the "done" callback should not be called? That is really the core of my question. In previous versions the behavior was different. – Dave L. Dec 19 '12 at 20:51
  • I would think yes, because it never finished. There is no data to return. The request was never sent, so it never succeeded. – Kevin B Dec 19 '12 at 20:52
2

The alert should not appear. http://api.jquery.com/jQuery.ajax/ says:

In particular, calling .abort() on the object will halt the request before it completes.

And in the code:

    // Allow custom headers/mimetypes and early abort
    if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) {
            // Abort if not done already and return
            return jqXHR.abort();

    }

I believe this was in response to bug #8193 and possibly bug #10944.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • +1 Definitely `bug #10944`, though I wonder why the changeset doesn't exist anymore. – Kevin B Dec 19 '12 at 20:55
  • @KevinB - [Here](https://github.com/jquery/jquery/commit/395612bb152660226b93f7f096cd0146fc06991e) is the changeset. – Dave L. Dec 19 '12 at 21:16