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的区别 #25

Open
pfan123 opened this issue Oct 18, 2018 · 0 comments
Open

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

pfan123 opened this issue Oct 18, 2018 · 0 comments

Comments

@pfan123
Copy link
Owner

pfan123 commented Oct 18, 2018

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

前端开发中经常会用到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块里提交。
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