如何在Play中调用非GET/POST HTTP方法? [英] How do I invoke non-GET/POST HTTP methods in Play?
问题描述
我有一个RESTful Play 2.0应用程序,我想从浏览器的链接中调用DELETE /sessions
.
I have a RESTful Play 2.0 application, and I'd like to call DELETE /sessions
from a link in the browser.
我该怎么做? Rails习惯于对非GET/POST请求使用_method
查询参数,但是Play的文档没有提及这一点.
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.
此外,我是否必须创建一个单独的表单或XMLHttpRequest
以便从链接的href
调用该方法,或者是否有类似Rails的data-method
属性的东西?
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?
推荐答案
您不会使用通用链接来做到这一点.
You will not do that with common link.
链接(A标记)不适用于DELETE方法.语义 链接的意思是链接"到另一个资源,该资源可以检索 使用GET请求.您可以在Ajax或表单中执行DELETE请求 使用POST方法并包含带有名称的隐藏输入 "X-HTTP-Method-Override"和值"DELETE".
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".
这是什么意思:您需要使用GET删除项目:
What is that mean: you need use GET to delete the item:
在conf/routes
中:
GET /delete/:itemId controllers.Application.deleteItem(itemid : Long)
您认为:
<a href="@routes.Application.deleteItem(item.id)">Delete @item.name</a>
或通过AJAX调用:
<script type="text/javascript">
$.ajax({
url:"@routes.Application.actionToDeleteSomething()",
type:"DELETE"
});
</script>
这篇关于如何在Play中调用非GET/POST HTTP方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!