There are a total of 7 HTTP request methods, including OPTIONS, HEAD, GET, POST, etc. (the message header has a picture, which is very clear)

Request method: specifies what the client wants to do with the specified resource/server.
 Let's introduce the request methods available in HTTP/1.1:


[GET: Get resource]
     The GET method is used to request the resource identified by the URI. The specified resource is parsed by the server and returns the response content (that is, if the requested resource is text, it will be returned as it is; if it is a program like CGI [Common Gateway Interface], it will return the output result after execution) .
     Most commonly used to query the server for some information. When necessary, query string parameters can be appended to the end of the URL in order to send the information to the server.
     A common mistake that occurs when using GET requests is the wrong format of the query string. The name and value of each parameter in the query string must be encoded using encodeURLComponent() before being placed at the end of the URL; and all name-value pairs must be separated by an ampersand (&), as in the following example:
         xhr. open("get","01.php?name=foodoir&age=21",true);
     The following function can assist in adding query string parameters at the end of an existing URL:

     function addURLParam(url,name,value){
        url += (url.indexOf("?") == -1 ? "?" : "&");
        url += encodeURIComponent(name) + "=" + encodeURIComponent(value);
        return url;
    }

    The addURLParam function accepts three parameters: the URL to which the parameter is to be added, the parameter's name, and the parameter's value.
    Below is an example of using this function to build a URL

    var url = "example.php";
    //add parameters
    url = addURLParam(url,"name","foodoir");
    url = addURLParam(url,"age","21");
    //initialize request
    xhr.open("get",url,false);

    
[POST: transfer entity text]
    The POST method is used to transfer the body of the entity.
    Although the body of the entity can also be transmitted by the GET method, it is generally not transmitted by the GET method, but by the POST method; although the GET method is very similar to the POST method, the main purpose of POST is not to obtain the body content of the response.
    The body of a POST request can contain a very large amount of data in any format. Here's an example:
         xhr.open("post","01.php",true);
    The second step in sending a POST request is to pass some data into the send method. Since XHR was originally designed to process XML, Therefore, XML DOM documents can also be processed here, and the incoming document will be serialized and submitted to the server as the request body.
    By default, the server does not treat POST requests and requests for submitting WEB forms the same. Let's look at the following code:

复制代码
复制代码
function(){
    var xhr = CreateXHR();
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4){//检测XHR的readyState属性
            if((xhr.status >= 200 && xhr.status <= 300) || xhr.status == 304){
                alert(xhr.responseText);
            }else{
                alert("Request was unsuccessful:" + xhr.status);
            }
        }
    };
    
    xhr.open("post","post.php",true);
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    var form = document.getElementById("ID");
    xhr.send(serialize(form));
}
复制代码
复制代码

    我们可以模仿XHR表单提交:首先将Content-Type头部信息设置为application/x-www-form-urlencoded,也就是表单提交时的类型,其次是以适当的格式创建一个字符串(POST数据格式与查询字符串的格式相同),如果需要将页面中表单的数据进行序列化,然后再通过XHR函数发送到服务器,那么可以使用序列化函数serialize(),(表单序列化,这里不作具体介绍)

在这里我们来比较GET方法和POST方法本质上的区别:

  1、GET方法用于信息获取,它是安全的(安全:指非修改信息,如数据库方面的信息),而POST方法是用于修改服务器上资源的请求;

  2、GET请求的数据会附在URL之后,而POST方法提交的数据则放置在HTTP报文实体的主体里,所以POST方法的安全性比GET方法要高;

  3、GET方法传输的数据量一般限制在2KB,其原因在于:GET是通过URL提交数据,而URL本身对于数据没有限制,但是不同的浏览器对于URL是有限制的,比如IE浏览器对于URL的限制为2KB,而Chrome,FireFox浏览器理论上对于URL是没有限制的,它真正的限制取决于操作系统本身;POST方法对于数据大小是无限制的,真正影响到数据大小的是服务器处理程序的能力。
    
【HEAD:获得报文首部】
    HEAD方法和GET方法一样,知识不返回豹纹的主体部分,用于确认URI的有效性及资源更新的日期时间等。
    具体来说:1、判断类型; 2、查看响应中的状态码,看对象是否存在(响应:请求执行成功了,但无数据返回); 3、测试资源是否被修改过
    HEAD方法和GET方法的区别: GET方法有实体,HEAD方法无实体。

【PUT:传输文件】
    PUT方法用来传输文件,就像FTP协议的文件上传一样,要求在请求报文的主体中包含文件内容,然后保存在请求URI指定的位置。但是HTTP/1.1的PUT方法自身不带验证机制,任何人都可以上传文件,存在安全问题,故一般不用。

【DELETE:删除文件】
    指明客户端想让服务器删除某个资源,与PUT方法相反,按URI删除指定资源
    
【OPTIONS:询问支持的方法】
    OPTIONS方法用来查询针对请求URI指定资源支持的方法(客户端询问服务器可以提交哪些请求方法)
    
【TRACE:追踪路径】
    客户端可以对请求消息的传输路径进行追踪,TRACE方法是让Web服务器端将之前的请求通信还给客户端的方法
    
【CONNECT:要求用隧道协议连接代理】
    CONNECT方法要求在与代理服务器通信时建立隧道,实现用隧道协议进行TCP通信。主要使用SSL(安全套接层)和TLS(传输层安全)协议把通信内容加密后经网络隧道传输。

更多参考资料:

  《Javascript高级程序设计(第三版)》第21章;

  《图解HTTP》第二章;

http://www.cnblogs.com/foodoir/p/5911099.html

 

 

 

 

客户端连上服务器后,向服务器发出获取某个 Web 资源的消息,称之为客户端向服务器发送了一个 HTTP 请求。一个完整的 HTTP 请求包括如下内容:
①请求行
②若干消息头(请求头)
③实体内容(请求体) 有可能没有

  • 请求方式

HTTP 中定义了 7 种请求方式:POST、GET、HEAD、OPTIONS、DELETE、TRACE、PUT。其中最常用的是 GET 和 POST

1,GET 请求

[1]从字面意思来说,GET 请求是用来向服务器端获取信息而发送的请求。
[2]没有特殊设置, 默认情况下浏览器发送的都是 GET 请求, 具体的一共有5种,点击超链接,表单提交没有设置method,表单提交设置method为get方式,在浏览器地址栏直接输入地址访问,ajax中设置请求方式为get。
[3]GET 请求也可以向服务器端发送请求参数, 形式是在 URL 地址后面加上?,请求参数名和值用=连接,多个请求参数之间使用&分隔。例如:GET /mail/1.html?name=abc&password=xyz HTTP/1.1。

需要注意的是:GET 方式所能够携带的数据是由限制的,其数据大小通常不能超过 4K, 不适于提交大量表单数据, 故而在表单的提交方式中首选 POST 方式。

2,POST 请求

[1]POST 请求的字面含义是向服务器端发送数据,仅在表单中设置method=”post”时,请求方式为 POST 方式,另外在 Ajax 应用中,可以指定请求方式为 POST。
[2]POST 请求会将请求参数放在请求体中,而不是 URL 地址后面,并且发送数据的大小是没有限制的。
关于GET和POST2个请求方式,记住一句话就好了:GET一般用来从服务器获取数据,POST一般用来向服务器发送数据。

  • 请求消息头

请求消息头简称请求头,用来向服务器报告浏览器端的一些基本信息。
常用请求头
代码 说明
Accept: text/html,image/* 我支持的数据类型
Accept-Charset: utf-8 支持的数据的编码字符集
Accept-Encoding: gzip 支持的压缩方式
Accept-Language: en-us,zh-cn 支持的语言
Host: localhost:8888 请求的主机名
Referer: http://www.baidu.com/index.jsp 发送请求的界面对应的 url 防盗链
User-Agent: Mozilla/4.0 浏览器的相关信息
Connection: keep-Alive 请求完成后,到是断开呢还是连接着
Date: Tue, 11 Jul 2000 18:23:51 GMT 发送请求的时间
Cookie: tt=123 对具体客户端所做的标记

  • 请求体

GET 请求没有请求体,POST 请求: 如果 form 表单提交的方式为 post,则表单项的数据以请求体的形式发送给服务器,没有大小限制。

http://blog.csdn.net/u011794238/article/details/46408051



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326069277&siteId=291194637