当客户无法发出PUT/DELETE请求时,建议采取什么替代方法? [英] What alternative is advised when a client can't make a PUT/DELETE request?

查看:408
本文介绍了当客户无法发出PUT/DELETE请求时,建议采取什么替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求实现RESTful API,并且我想知道当浏览器客户端无法向服务器执行PUT/DELETE请求时建议使用什么参数. (由于一些浏览器显然没有支持这些请求)

它是否为此存在特定的参数名称? (类似于POST请求上的request_method = DELETE,它应该向服务器表明客户端确实想要DELETE但不能指示它)?还是没有这样的规格?

我的搜索使我找到了prototype.js使用的参数"_method".但这是标准参数吗? 例如,jQuery如何处理它?<​​/p>

感谢您的帮助.

解决方案

您本质上在询问的问题称为方法重载-尽管它不是100%RESTful的,但您可以回退到该方法如果客户端不支持PUT/DELETE方法.理查森/Ruby的书 RESTful Web服务提到了这种方法也是

您自己在问题中创造了基本原理-与完全RESTful方法相比,区别在于您引入了一个新参数,该参数通过POST作为请求正文的一部分提交.此参数表示通过(重载)POST请求提交的预期方法(例如POST/PUT/DELETE).这使您可以克服客户端的HTTP方法限制,同时仍保持URI设计的整洁和RESTful(例如,不带动词).但是,您仍然应该尽可能地实现RESTful,即继续对只读请求使用GET(从而不影响缓存等),并对任何写请求使用重载的post.至于如何命名包含真正HTTP方法的参数-我自己不知道任何标准化的命名策略,因此应该使用Prototype.js使用的名称(或使用适合您的命名策略的任何名称)很好.

关于jQuery和 XmlHttpRequest ,它们通常应支持PUT和DELETE方法.参见:

请注意,jQuery文档警告有关对PUT/DELETE的支持取决于浏览器.据我了解,使用XHR时应支持这些方法...但是,您应该在POST重载与完整的HTTP方法堆栈之间做出决定之前,测试所有目标浏览器是否支持这些方法.

默认情况下,

jQuery提供对.get().post()方法的访问,这两个方法都建立在.ajax()之上.为了通过jQuery进行PUT/DELETE调用,您需要直接使用.ajax()方法,并在设置的type中指定该方法.但这确实是一个真正的PUT/DELETE请求,因为jQuery本身不提供任何自动方法来处理方法重载(因此,您需要自己编写使其适合API设计).

如果您将不使用XHR并且需要依赖标准格式,那么恐怕您将不得不使用方法重载,因为从 HTML5规范(本在您所指的SO问题中也有提到).

I'm looking to implement a RESTful API and I was wondering what parameter is advised when the browser client can't do a PUT/DELETE request to the server. (since some browser apparently doesn't support these requests)

Does it exist a specific parameter name for that? (something like request_method=DELETE on a POST request, that should indicate to the server that the client really want a DELETE but can't indicate it)? or there is no specifications like that?

My searches lead me to the parameters "_method" used by prototype.js. But is this parameters a standard? For example, how jQuery handle it?

Thanks for your help.

解决方案

What you are essentially asking about is called method overloading - although it's not 100% RESTful, it's an approach which you can fallback to in case that the client doesn't support PUT/DELETE methods. Richardson's/Ruby's book RESTful Web Services mentions this method as well.

You coined the basic principle yourself in your question - the difference when compared to the fully RESTful approach is that you introduce a new parameter which is submitted as part of the request body via POST. This parameter indicates the intended method (e.g. POST/PUT/DELETE) which is being submitted via an (overloaded) POST request. This allows you to overcome the HTTP method limitation of the client while still keeping your URI design clean and RESTful (e.g. without verbs). However you should still aim to be as much RESTful as possible, i.e. keep using GET for read-only requests (thus not impacting caching, etc) and use overloaded post for any write requests. As to how to name the parameter which holds the true HTTP method - I'm not aware of any standardized naming policies myself, so going with what's used by Prototype.js (or with any name that fits well into your naming policy) should be fine.

Regarding jQuery and XmlHttpRequest in general, these should support PUT and DELETE methods. See:

Note that the jQuery documentation warns about support of PUT/DELETE being browser-dependent. The way I understand it is that these methods should be supported when using XHR ... however you should rather test if all your target browsers support these methods before making a decision between POST overloading vs. the full HTTP method stack.

jQuery by default provides access to .get() and .post() methods which are both built on top of .ajax(). For doing PUT/DELETE calls via jQuery you need to use .ajax() method directly and specify the method in type in settings. This however does a true PUT/DELETE request, as jQuery itself does not provide any automatic means how to handle method overloading (so you need to code it yourself so that it fits into your API design).

If you won't be using XHR and need to rely on standard forms, then I'm afraid that you will have to use method overloading because support for PUT/DELETE methods as valid form action values has been dropped from the HTML5 spec (this was also noted in the SO question which you are referring to).

这篇关于当客户无法发出PUT/DELETE请求时,建议采取什么替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆