backbone-tastypie icon indicating copy to clipboard operation
backbone-tastypie copied to clipboard

Delete method in tastypie returns an empty 204 response and this doesn't play nice with backbone.

Open jorgeecardona opened this issue 11 years ago • 7 comments

Hi,

Currently tastypie response for DELETE's is a 204 but backbone is waiting for something there, maybe we just need an extra if in the sync defined by backbone-tastypie.

Look at: http://stackoverflow.com/questions/6988873/backbone-model-destroy-not-triggering-success-function-on-success

Bye

jorgeecardona avatar Jan 24 '13 03:01 jorgeecardona

I've never had any issue with this. Did you experience this yourself? Do you have a testcase or something else that shows this behavior?

PaulUithol avatar Feb 26 '13 23:02 PaulUithol

I will try to build a testcase, but the idea is that tastypie is returning a 204 response and it;s jquery ajax who is actually calling the error callback, I'm using 1.9.0.js. Backbone-tastypie code seems to be ok, look at http://code.jquery.com/jquery-1.9.0.js in line 8159:

try {
  response = conv( response );
} catch ( e ) {
  return { state: "parsererror", error: conv ? e : "No conversion from " + prev + " to " + current };
}

That try catch is excecuted and conv function try to parse as json the empty string which return an error, don't know if this is jquery, backbone, or just my error.

I just have a tastypie api with the DELETE method enabled, returning 204 with empty content

jorgeecardona avatar Feb 27 '13 00:02 jorgeecardona

I had the same problem, my solution was like this:

deleteEmpleado: function() {
    this.model.destroy({
        complete: function(objeto, exito){  
            if (objeto.status == 204 ) {
                window.history.back();
            }else {
                console.log( objeto.status ); 
            }
        }
    });
}

here the app: https://github.com/diegofer/app_django_backbone here the app online: http://dieguisimo.alwaysdata.net/

diegofer avatar Feb 27 '13 16:02 diegofer

I am having the same issue but also with post and patch (any request that returns a blank response body). Server returns 201 CREATED (post) 202 ACCEPTED (patch) however jquery is throwing an ajaxError because response body is blank. See: http://jquery.com/upgrade-guide/1.9/#jquery-ajax-returning-a-json-result-of-an-empty-string. Here is a stackoverflow question discussing this issue: http://stackoverflow.com/questions/14468704/what-changed-in-jquery-1-9-to-cause-a-ajax-call-to-fail-with-syntax-error.

The above workarounds are okay for patch and delete but this is a much larger issue for post requests as backbone-tastypie is not getting the newly created object from the http location response header.

This was not an issue for me with earlier versions of jquery/backbone. My current version javascript library versions are: jquery: 1.9.1 backbone: 1.0.0 (was also broke in 0.9.10 as well) backbone-tastypie: bdac458a24d6031d2f1e65f0ebd850e80a3c6ffe

This is not really a bug in backbone-tastypie but rather more of a jquery issue. That said, I think the workaround likely belongs in backbone-tastypie. Here is a suggestion on a possible fix: https://github.com/jquery/jquery-migrate/blob/master/warnings.md#jqmigrate-jqueryparsejson-requires-a-valid-json-string.

samkuehn avatar Mar 21 '13 16:03 samkuehn

@samkuehn +1

glenrobertson avatar Mar 27 '13 02:03 glenrobertson

I added the following at the beginning of backbone-tastypie, which appears to fix the issue for now. I did not run any tests, though.

jQuery.ajaxSetup({dataFilter: function(data, type) {
    if (type == "json" && data == "") {
        data = null;
    }
    return data;
}});

tommikaikkonen avatar Apr 03 '13 12:04 tommikaikkonen

Worked for me thanks @tommikaikkonen.

samkuehn avatar Apr 03 '13 14:04 samkuehn