Jake Petroules
Jake Petroules

Reputation: 24160

Why do neither "done" or "fail" jQuery AJAX callbacks execute on an HTTP 500?

I've got a little jQuery here:

$.ajax({
    url: $this.fileUploadUrl,
    data: 'url=' + encodeURIComponent(file.name),
    type: 'POST',
    done: function () {
        file.status = plupload.DONE;
        $this.updateFileStatus(file);
    },
    fail: function () {
        file.status = plupload.FAILED;
        $this.updateFileStatus(file);
    }
});

If the server returns a HTTP 500 response, the fail callback does NOT run, and neither does done. I even tried adding always, which didn't work either. What am I missing?

Upvotes: 1

Views: 2559

Answers (2)

Travis J
Travis J

Reputation: 82287

var request = $.ajax({
  url: $this.fileUploadUrl,
  type: "POST",
  data: 'url=' + encodeURIComponent(file.name)
});

request.done(function() {
  file.status = plupload.DONE;
  $this.updateFileStatus(file);
});

request.fail(function() {
  file.status = plupload.FAILED;
  $this.updateFileStatus(file);
});

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385194

What are done and fail? The documentation does not list them.

(They are member functions of the jqXHR object, but that's not the same as them being options in a call to $.ajax().)

Perhaps you're looking for success and error, respectively:

$.ajax({
    url: $this.fileUploadUrl,
    data: 'url=' + encodeURIComponent(file.name),
    type: 'POST',
    success: function(data, textStatus, jqXHR) {
        file.status = plupload.DONE;
        $this.updateFileStatus(file);
    },
    error: function(jqXHR, textStatus, errorThrown) {
        file.status = plupload.FAILED;
        $this.updateFileStatus(file);
    }
});

Or, to keep your original terminology, the following (which is not quite equivalent but close-ish):

$.ajax({
    url: $this.fileUploadUrl,
    data: 'url=' + encodeURIComponent(file.name),
    type: 'POST'
}).done(function() {
    file.status = plupload.DONE;
    $this.updateFileStatus(file);
}).fail(function() {
    file.status = plupload.FAILED;
    $this.updateFileStatus(file);
});

Upvotes: 5

Related Questions