版权声明:本文为博主原创文章,未经博主允许不得转载。

目录(?)[+]

一般来说,Web服务器默认的只支持Post和Get这两种“只读”的请求方法。但是随着Ajax XMLHttpRequest 和 REST风格应用的深入,我们发现Http 1.1协议还支持如下请求方法(Request Method):

  • OPTIONS
  • HEAD
  • DELETE
  • PUT
  • TRACE
  • CONNECT
Get是最常用的,就是向Web Server发请求“获取”资源;那么Post就是向Web Server“邮寄”一些封装的数据包获取资源,这两者方法严格的说都是“索取”行为。
顾名思义,Delete方法就是通过http请求删除指定的URL上的资源啦,Delete请求一般会返回3种状态码:
  • 200 (OK) - 删除成功,同时返回已经删除的资源
  • 202 (Accepted) - 删除请求已经接受,但没有被立即执行(资源也许已经被转移到了待删除区域)
  • 204 (No Content) - 删除请求已经被执行,但是没有返回资源(也许是请求删除不存在的资源造成的)

Put方法就不多废话了,就是往Web Server上直接扔资源(上传资源)嘛,不过实际操作起来可能会让诸位看官喝一壶,E文定义如下:

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI. If a new resource is created, the origin server MUST inform the user agent via the 201 (Created) response. If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request. If the resource could not be created or modified with the Request-URI, an appropriate error response SHOULD be given that reflects the nature of the problem. The recipient of the entity MUST NOT ignore any Content-* (e.g. Content-Range) headers that it does not understand or implement and MUST return a 501 (Not Implemented) response in such cases.

If the request passes through a cache and the Request-URI identifies one or more currently cached entities, those entries SHOULD be treated as stale. Responses to this method are not cacheable.

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource. If the server desires that the request be applied to a different URI,

it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request.

A single resource MAY be identified by many different URIs. For example, an article might have a URI for identifying "the current version" which is separate from the URI identifying each particular version. In this case, a PUT request on a general URI might result in several other URIs being defined by the origin server.

HTTP/1.1 does not define how a PUT method affects the state of an origin server.

PUT requests MUST obey the message transmission requirements set out in section 8.2.

Unless otherwise specified for a particular entity-header, the entity-headers in the PUT request SHOULD be applied to the resource created or modified by the PUT.

上面说的都是虚的,实战才是硬道理!

(本文始发于CSDN,作者胡奇的博客:http://blog.csdn.net/kthq

  • 首先,我们要让Web Server支持Delete 和 Put请求方法,以大家熟悉的Tomcat为例:

Tomcat的web.xml 文件中配置 org.apache.catalina.servlets.DefaultServlet 的初始化参数

  1. <init-param>
  2. <param-name>readonly</param-name>
  3. <param-value>false</param-value>
  4. </init-param>

readonly参数默认是true,即不允许delete和put操作,所以默认的通过XMLHttpRequest对象的put或者delete方法访问就会报告 http 403 forbidden 错误。

  • 接下来,从客户端通过 Ajax XMLHTTPRequest 发起 DELETE/PUT 请求:

    1. function getXMLHTTPRequest(){
    2. if (XMLHttpRequest)    {
    3. return new XMLHttpRequest();
    4. } else {
    5. try{
    6. return new ActiveXObject('Msxml2.XMLHTTP');
    7. }catch(e){
    8. return new ActiveXObject('Microsoft.XMLHTTP');
    9. }
    10. }
    11. }
    12. var req = getXMLHTTPRequest();
    13. req.open('DELETE','http://localhost/test.jsp',false);
    14. req.send(null);
    15. document.write(req.responseText);
  • WebDAV也需要使用到这2种Http请求方法。

RequestMethod 相关的更多相关文章

  1. 嵌入式单片机STM32应用技术(课本)

    目录SAIU R20 1 6 第1页第1 章. 初识STM32..................................................................... ...

  2. RequestMethod.DELETE相关,如何用jquery实现RequestMethod.DELETE请求

    Spring MVC添加支持Http的delete.put请求!(HiddenHttpMethodFilter) Spring3.0之后->Spring MVC过滤器-HiddenHttpMet ...

  3. JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识

    JAVA WEB快速入门系列之前的相关文章如下:(文章全部本人[梦在旅途原创],文中内容可能部份图片.代码参照网上资源) 第一篇:JAVA WEB快速入门之环境搭建 第二篇:JAVA WEB快速入门之 ...

  4. SpringCloud系列六:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)

    1.概念:Feign 接口服务 2.具体内容 现在为止所进行的所有的 Rest 服务调用实际上都会出现一个非常尴尬的局面,例如:以如下代码为例: Dept dept = this.restTempla ...

  5. 项目总结之Oauth2.0免登陆及相关知识点总结

    简介Oauth2.0授权步骤 授权码模式的基本步骤 原文链接地址 (A)用户访问客户端,后者将前者导向认证服务器. (B)用户选择是否给予客户端授权. (C)假设用户给予授权,认证服务器将用户导向客户 ...

  6. SpringMVC中与Spring相关的@注解

    一.Spring的常用组件类注解 @Component    被该注解所修饰的类是一个普通的spring bean类,该注解可以替代@Controller.@Service.@Repository.在 ...

  7. 【微信公众号开发】【10】JSJDK相关

    前言: 1,优点:官方提供的,会调用后还算使用方便,不用费劲了解各个原生组件 缺点:使用上有限制(如:上传文件有大小限制),很容易踩坑,部分安卓手机及电脑端不支持pjax 总结:上手容易,坑很多 2, ...

  8. Spring: 读取 .properties 文件地址,json转java对象,el使用java类方法相关 (十三)

    1. 在Java中获取 .properties 文件的路径 (src/main/resources 下) ProjectName |---src/main/java |---src/main/reso ...

  9. spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

    spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...

随机推荐

  1. mysql创建表

    说明:此文件包含了blog数据库中建立所有的表的Mysql语句. 在sql语句中注意“约束的概念": 1.实体完整性约束(主键--唯一且非空) primary key() 违约处理:No a ...

  2. javascript编程习惯总结

    总结一下javascript需要注意的一些细节的地方: 1:声明一个对象:var a={ }; 2:另一个对象继承一个对象:Var b=Object.create(a);如果父类的属性和方法发生改变, ...

  3. 部署Zipkin分布式性能追踪日志系统的操作记录

    Zipkin是Twitter的一个开源项目,是一个致力于收集Twitter所有服务的监控数据的分布式跟踪系统,它提供了收集数据,和查询数据两大接口服务. 部署Zipkin环境的操作记录:部署Zipki ...

  4. html中的src与href的区别

    写代码的时候就经常把这两个属性弄混淆,到底是href还是src,href标识超文本引用,用在link和a等元素上,href是引用和页面关联,是在当前元素和引用资源之间建立联系,src表示引用资源,表示 ...

  5. immutability-javascript

    https://www.sitepoint.com/immutability-javascript/

  6. StaticFileMiddleware中间件如何处理针对文件请求

    StaticFileMiddleware中间件如何处理针对文件请求 我们通过<以Web的形式发布静态文件>和<条件请求与区间请求>中的实例演示,以及上面针对条件请求和区间请求的 ...

  7. Linux Linux程序练习九

    题目:利用多线程与有名管道技术,实现两个进程之间发送即时消息,实现聊天功能 思路:关键在于建立两个有名管道,利用多线程技术,进程A中线程1向管道A写数据,进程B中线程2从管道A读数据,进程A线程2从管 ...

  8. php基础15:表单验证

    <?php $nameError=$emailError=$genderError=$websiteError=""; $name=$email=$gender=$comme ...

  9. 反射中使用 BindingFlags.IgnoreCase

    我们经常需要通过反射来调用目标对象的方法或获取/设置目标对象的属性,Type.InvokeMember 方法签名如下: public object InvokeMember(string name,B ...

  10. 淘宝SKU组合查询算法实现

    淘宝SKU组合查询算法实现 2015-11-14 16:18 1140人阅读 评论(0) 收藏 举报  分类: JavaScript(14)    目录(?)[+]   前端有多少事情可以做,能做到多 ...