Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GET 和 POST 请求的区别 #47

Open
oliver1204 opened this issue Jul 11, 2017 · 0 comments
Open

GET 和 POST 请求的区别 #47

oliver1204 opened this issue Jul 11, 2017 · 0 comments

Comments

@oliver1204
Copy link
Owner

oliver1204 commented Jul 11, 2017

普通常见的区别

  1. GET在浏览器回退时是无害的,而POST会再次提交请求。
  2. GET产生的URL地址可以被Bookmark,而POST不可以。
  3. GET请求会被浏览器主动cache,而POST不会,除非手动设置。
  4. GET请求只能进行url编码,而POST支持多种编码方式。
  5. GET请求参数会被完整保留在浏览器历史记录里,而POST中的参数不会被保留。
  6. GET请求在URL中传送的参数是有长度限制的,而POST么有。
  7. 对参数的数据类型,GET只接受ASCII字符,而POST没有限制。
  8. GET比POST更不安全,因为参数直接暴露在URL上,所以不能用来传递敏感信息。
  9. GET参数通过URL传递,POST放在Request body中。

区别详解

1. 对于第四条

因为GET请求只能进行url编码,所以在使用get方法发送请求的时候,如果参数是数组的情况,需要注意转码问题。例如:

let params = {
     refundArry: [1,2],
     statusArry: [1,2]
}
$http({
     'method': 'GET',
     'url': babala,
     'params': params,
})
Quert String Parameters
refundArry:1
refundArry:0
statusArry:0
statusArry:-1
type:1
uid:64

这一定不是我们想要的,所以一定要经过下面的方式转换:

let params = {
     refundArry: [1,2],
     statusArry: [1,2]
}

// 将数组转化成字符串,但是形似数组,例如:[1,2]=> "[1,2]"
let param = {}
for(var key in params) {
    if (angular.isArray(params[key])) {
        param[key] = `[ ${params[key]} ]`
    } else {
        param[key] = params[key]
    }
}

$http({
     'method': 'GET',
     'url': babala,
     'params': param,
})
Quert String Parameters
refundArry:[1,2]
statusArry: [1,2]
type:1
uid:64

这样就是我们想要的结果了

2. 对于第六条

参数大小的限制又是从哪来的呢?

GET和POST还有一个重大区别,GET产生一个TCP数据包;POST产生两个TCP数据包。

对于GET方式的请求,浏览器会把http header和data一并发送出去,服务器响应200(返回数据);

而对于POST,浏览器先发送header,服务器响应100 continue,浏览器再发送data,服务器响应200 ok(返回数据)。

据研究,在网络环境好的情况下,发一次包的时间和发两次包的时间差别基本可以无视。而在网络环境差的情况下,两次包的TCP在验证数据包完整性上,有非常大的优点。

并不是所有浏览器都会在POST中发送两次包,Firefox就只发送一次。
image
image

@oliver1204 oliver1204 changed the title HTTP(五) -- get 请求发生数组的情况 GET 和 POST 请求的区别 Jul 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant