How do you send a request with the “DELETE” HTTP verb?

后端 未结 4 1160
温柔的废话
温柔的废话 2020-12-29 00:09

I\'d like to create a link in a view within a Rails application that does this...

DELETE /sessions

How would I do that.

Added compl

4条回答
  •  -上瘾入骨i
    2020-12-29 00:30

    I don't know about Rails specifically, but I frequently build web pages which send DELETE (and PUT) requests, using Javascript. I just use XmlHttpRequest objects to send the request.

    For example, if you use jQuery:

    have a link that looks like this:

    delete
    

    And run this Javascript:

    $(function(){
        $('a.delete').click(function(){
            $.ajax(
                {
                    url: this.getAttribute('href'),
                    type: 'DELETE',
                    async: false,
                    complete: function(response, status) {
                        if (status == 'success')
                            alert('success!')
                        else
                            alert('Error: the service responded with: ' + response.status + '\n' + response.responseText)
                    }
                }
            )
            return false
        })
    })
    

    I wrote this example mostly from memory, but I'm pretty sure it'll work....

提交回复
热议问题