Ticket #11476 (closed bug: duplicate)
Ajax context is lost in deferred
| Reported by: | thanatica | Owned by: | |
|---|---|---|---|
| Priority: | undecided | Milestone: | None |
| Component: | unfiled | Version: | 1.7.2rc1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description (last modified by jaubourg) (diff)
When using $.ajax as a deferred, the context is lost when the deferred succeeds and does its callback. Consider the following code:
var context = { foo: "bar" };
$.when($.ajax({ url: "...", context: context })).then(function(args) {
//Where is context gone?
});
At the point of the comment, there is no trace of the original context object. It seems lost. According to the documentation, it should be in args[0], but args is holding [ returned data, "success", jqXHR ].
Now of course, in this example. it's still accessible due to its scope, but the whole point is to keep it accessible even if it gets out of scope.
Change History
comment:1 Changed 14 months ago by jaubourg
- Status changed from new to closed
- Resolution set to worksforme
- Description modified (diff)
comment:3 Changed 10 months ago by rwaldron
You can only ever have one context, so why would even do that to begin with? I agree that it shouldn't be "undefined", but why would you jump through all of that code? If you want to set an explicit context object, use $.proxy:
comment:4 Changed 10 months ago by jaubourg
- Status changed from closed to reopened
- Resolution worksforme deleted
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Works for me: http://jsfiddle.net/kQMyz/