日期:2014-05-20  浏览次数:20844 次

REST架构下,浏览器怎么发送put与delete请求?
例如后台已经搞定,REST服务已经建好了,那么前台浏览器怎么使用呢?

------解决方案--------------------
前端比较麻烦,因为:
The other HTTP methods (i.e. other than GET and POST) are not available in HTML 4.1 or XHTML 1.0.

也就是说实际上HTML5以前,FORM都仅支持GET和POST。


即便你尝试自己用Ajax来做,都未必能成功。
在jQuery的文档中说:
The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.(最后一句话:不是所有浏览器支持其它方式)
------解决方案--------------------
可以用POST来代替PUT和DELETE, 比如你可以埋一个hidden field叫 _method, <input type= "hidden " name= "_method " value= "PUT "> . 这样,你在后台可以根据这个字段来识别.