2

In Backbone, there is a "DELETE".

this.model.url = '/js/products/123/destroy';
this.model.destroy();

How can I emulate this in JQuery?

In my node.js app, I specifically look for app.delete.

.   Request URL:http://mydomain.com/js/products/2/destroy
.   Request Method:DELETE
.   Status Code:200 OK
.   Request Headersview source
.   Accept:application/json, text/javascript, */*; q=0.01
.   Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
.   Accept-Encoding:gzip,deflate,sdch
.   Accept-Language:en-US,en;q=0.8
.   Connection:keep-alive
.   Cookie:connect.sid=s%3ALmcKt2dNgSzfQCyCAcVlPeg6.bFXUPlpuZctDpfN2Gu5mtslVC9nA3FOi908Qe8aExYY
.   Host:mydomain.com
.   Origin:http://mydomain.com
.   Referer:http://mydomain.com/1
.   User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31 AlexaToolbar/alxg-3.1
.   X-Requested-With:XMLHttpRequest
.   Response Headersview source
.   Connection:keep-alive
.   Content-Length:27
.   Content-Type:text/html; charset=utf-8
.   Date:Sun, 14 Apr 2013 02:57:17 GMT
.   X-Powered-By:Express
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 1
    What does your JS console say about the AJAX request that Backbone sends when you delete that object? – Blender Apr 14 '13 at 02:53

1 Answers1

9

have a look here:

In your case that would translate to:

$.ajax({
    url: '/js/products/123/destroy',
    type: 'DELETE',
    success: function(result) {
        // Do something with the result
    }
});
Community
  • 1
  • 1
Mathijs Flietstra
  • 12,900
  • 3
  • 38
  • 67
  • 2
    The `$.ajax()` documentation still includes a warning for 'type', "Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers." – Beetroot-Beetroot Apr 14 '13 at 03:03
  • @Beetroot-Beetroot Right, but just to be clear, that's nothing specific to jQuery...just AJAX/forms in general. This might be a good reference: http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers – Ian Apr 14 '13 at 03:10
  • 1
    @Beetroot-Beetroot, thanks, I didn't know about that. Have a look [here](http://stackoverflow.com/a/2456851/1846192) for an alternative approach. It makes mention of some issues with IE7 and IE8. But what I can gather from the comments on the linked post is that it should work fine with an uppercase 'DELETE' in > IE6. – Mathijs Flietstra Apr 14 '13 at 03:12
  • 1
    @Beetroot-Beetroot If [`emulateHTTP`](http://backbonejs.org/#Sync-emulateHTTP) is needed, then the equivalent is `type: 'POST', data: { _method: 'DELETE' }`. – Jonathan Lonowski Apr 14 '13 at 03:28
  • I think the warning in the jQuery documentation is worth heeding. Unless you are targeting a particular client environment that definitely supports DELETE, then the safe assumption, given current and legacy bowsers that are out there (and Doctypes), has to be that DELETE requests *may* fail. Personally, I would make client-side and server-side adaptations to allow the POST method to be used. – Beetroot-Beetroot Apr 14 '13 at 03:49