使用 HTML 进行 HTTP DELETE
在webapp2中,我一直无法正确使用HTTP DELETE请求。作为一种变通办法,我用GET请求了一个不同的URI,但我更希望能采用更符合REST风格的方法。
使用这段代码时,服务器日志显示DELETE请求实际上被当作了GET请求。我这里缺少了什么呢?
class TeamPages(webapp2.RequestHandler):
def get(self, team_name):
...
def post(self, team_name):
...
def delete(self, team_name):
key_name = team_name.upper()
Team.delete(key_name)
self.redirect('/teams')
对/teams/{{ team_name }}的GET请求会返回一个包含以下html的页面,但当我提交时,它请求的是GET方法,而不是DELETE方法。
<form action="/teams{{ team.team_name }}" method="delete">
<button type="submit">Delete</button>
</form>
更新
补充信息... 我在Google App Engine上开发,使用的是Mac上的Chrome浏览器。这里是请求头,显示的是GET而不是DELETE...
GET /teams/hornets? HTTP/1.1
Host: localhost:9080
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: dev_appserver_login="test@example.com:True:185804764220139124118"
Referer: http://localhost:9080/teams/hornets
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
这是响应头...
HTTP/1.1 405 Method Not Allowed
allow: DELETE
Cache-Control: no-cache
Content-Length: 187
content-type: text/html; charset=UTF-8
Date: Tue, 10 Jun 2014 21:24:36 GMT
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Server: Development/2.0
1 个回答
1
简单来说:问题不在于网页框架或浏览器,而是HTML本身的限制。HTML只允许使用GET和POST这两种方法,其他的方法都不被允许。我已经调整了我的标题,以更准确地反映这个话题。
更多细节:
根据@Greg提供的HTML线索,我进行了进一步的搜索。在一篇相关帖子的评论中,@Guandalino分享了一些很有用的链接:
这些链接帮助我们更好地理解了在HTML规范中添加PUT和DELETE的支持的争论。
对我来说(我还是个新手),这种与HTTP的不兼容似乎与REST原则不太一致。我觉得,考虑到这么多网页框架已经找到了解决办法,这就足以成为未来HTML版本中将其标准化的理由。
感谢大家的反馈!