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

HTTP请求中的Form Data与Request Payload的区别 #105

Open
rainnaZR opened this issue Aug 10, 2017 · 21 comments
Open

HTTP请求中的Form Data与Request Payload的区别 #105

rainnaZR opened this issue Aug 10, 2017 · 21 comments

Comments

@rainnaZR
Copy link

rainnaZR commented Aug 10, 2017


title: HTTP请求中的Form Data与Request Payload的区别
date: 2017-08-10

前端开发中经常会用到AJAX发送异步请求,对于POST类型的请求会附带请求数据。而常用的两种传参方式为:Form Data 和 Request Payload。

121212

334343

GET请求

使用get请求时,参数会以key=value的形式拼接在请求的url后面。例如:

http://m.baidu.com/address/getlist.html?limit=50&offset=0&t=1502345139870

但是受限于请求URL的长度限制,一般参数较少时会使用get请求。

POST请求

当参数数量较多,且对数据有一定安全性要求时,会考虑用post请求传递参数数据。POST请求的参数数据是在请求体中。

方式一: Form Data形式

当POST请求的请求头里设置Content-Type: application/x-www-form-urlencoded(默认), 参数在请求体以标准的Form Data的形式提交,以&符号拼接,参数格式为key=value&key=value&key=value...

3333

121212

前端代码设置:

xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send('a=1&b=2&c=3');

在servlet中,后端可以通过request.getParameter(name)的形式来获取表单参数。

方式二:Request Payload形式

如果使用AJAX原生POST请求,请求头里设置Content-Type:application/json,请求的参数会显示在Request Payload中,参数格式为JSON格式:{"key":"value","key":"value"...},这种方式可读性会更好。

444

334343

后端可以使用getRequestPayload方法来获取。

Form Data 和 Request Payload 区别

  1. 如果请求头里设置Content-Type: application/x-www-form-urlencoded,那么这个请求被认为是表单请求,参数出现在Form Data里,格式为key=value&key=value&key=value...

  2. 原生的AJAX请求头里设置Content-Type:application/json,或者使用默认的请求头Content-Type:text/plain;参数会显示在Request payload块里提交。

参考文档:
http://www.cnblogs.com/btgyoyo/p/6141480.html
http://xiaobaoqiu.github.io/blog/2014/09/04/form-data-vs-request-payload/

@yzfdjzwl
Copy link

yzfdjzwl commented Feb 2, 2018

Great~

@supgeek-rod
Copy link

good job ~

@freestyledash
Copy link

thanks

@uyarn
Copy link

uyarn commented Mar 21, 2018

great

@Moriarty16
Copy link

Quite helpful! Thanks, man.

@kaneruan
Copy link

kaneruan commented May 3, 2018

还有一种类型,比如上传图片。
2018-05-03 4 30 47

@GenweiWu
Copy link

GenweiWu commented Jul 3, 2018

@80666881
Copy link

学习了

@zhonghuasheng
Copy link

Great

@xuxinhang
Copy link

还有一种类型,比如上传图片。
2018-05-03 4 30 47

比较有意思的是,用了 FormData API 或者 mutipart/form-data, 请求反而放在了 “Payload“ 下面。

@ql434
Copy link

ql434 commented Jan 22, 2019

mark

@vuuihc
Copy link

vuuihc commented Feb 12, 2019

文章只是描述了表象,并没有讲这两种区别的更深入的内容。比如为什么有这种区分

@chao15658
Copy link

了解。多谢

@howtomakeaturn
Copy link

this saves my day! thanks a lot!

@ghost
Copy link

ghost commented Aug 14, 2019

解决了我的困惑

@LuVx21
Copy link

LuVx21 commented Mar 16, 2020

学习了!

@zeguangzhang
Copy link

post也一样不安全啊,只不过没有那么明显罢了

@pei-han
Copy link

pei-han commented May 30, 2020

GET
可以附带form data或者其他格式的payload body吗?

@ghost
Copy link

ghost commented Sep 24, 2020

3ku!

@Fxskyzz
Copy link

Fxskyzz commented Jan 29, 2021

so great!

@codingbylch
Copy link

thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests