Delete method in tastypie returns an empty 204 response and this doesn't play nice with backbone.
Opened this issue · 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.
Bye
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?
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
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/
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: bdac458
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 +1
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;
}});
Worked for me thanks @tommikaikkonen.