How do I invoke non-GET/POST HTTP methods in Play?

2019-07-10 11:08发布

I have a RESTful Play 2.0 application, and I'd like to call DELETE /sessions from a link in the browser.

How do I do this? Rails has a convention of using a _method query parameter for non-GET/POST requests, but Play's documents don't mention this.

Also, do I have to create a separate form or XMLHttpRequest in order to invoke the method from a link's href or is there something like Rails's data-method attribute?

1条回答
你好瞎i
2楼-- · 2019-07-10 11:44

You will not do that with common link.

Cite from similar topic on Google Groups:

A link (A tag) is not intended to work with DELETE method. The semantic of a link is to “link” to another resource, which can be retrieve using a GET request. You can do DELETE requests in Ajax, or in form using a POST method and containing an hidden input with name “X-HTTP-Method-Override” and value “DELETE”.

What is that mean: you need use GET to delete the item:

in conf/routes:

GET   /delete/:itemId    controllers.Application.deleteItem(itemid : Long)

in your view:

<a href="@routes.Application.deleteItem(item.id)">Delete @item.name</a>

Or with AJAX call:

<script type="text/javascript">
    $.ajax({
        url:"@routes.Application.actionToDeleteSomething()",
        type:"DELETE"
    });
</script>
查看更多
登录 后发表回答