HTTP请求中的form data和request payload的区别

如题所述

区别就是:

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

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

拓展资料:

超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。所有的WWW文件都必须遵守这个标准。设计HTTP最初的目的是为了提供一种发布和接收HTML页面的方法。1960年美国人Ted Nelson构思了一种通过计算机处理文本信息的方法,并称之为超文本(hypertext),这成为了HTTP超文本传输协议标准架构的发展根基。Ted Nelson组织协调万维网协会(World Wide Web Consortium)和互联网工程工作小组(Internet Engineering Task Force )共同合作研究,最终发布了一系列的RFC,其中著名的RFC 2616定义了HTTP 1.1。

参考资料:

HTTP—百度百科

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-06-24

区别如下:

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

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

工作原理:

HTTP是基于客户/服务器模式,且面向连接的。典型的HTTP事务处理有如下的过程。

1、客户与服务器建立连接。

2、客户向服务器提出请求。

3、服务器接受请求,并根据请求返回相应的文件作为应答。

4、客户与服务器关闭连接。

以上内容参考:百度百科——HTTP

本回答被网友采纳
第2个回答  2014-11-19
今天帮朋友调试一个程序时发现用jQuery的ajax方法和post方法分别发送请求,在后台Servlet进行处理时结果是不一样的,比如用$.ajax方法发送请求时(data参数是一个JSON.stringify()处理后的字符串,而不是一个JSON对象),servlet里可以这样使用Gson来解析:
new Jsonparser().parse(request.getReader())

但此时是不可用request.getParam(key) 来取值的。
如果用$.post方法来发送请求(data参数是一个JSON对象,而不要再用JSON.stringify()处理为字符串了),结果恰恰相反。
在Chrome中调试发现,$.ajax发送的请求显示在request payload下面,而使用$.post方法发送的请求显示在form data下面。有什么区别呢?在万能的stackoverflow上找到答案了,有人问了这个问题What is the difference between form data and request payload?:
When I send an AJAX Post request and send parameters in queryString in send() method,Chrome Developer Tool’s XHR capture tool shows the parameters under request payload. and when I use jquery’s post function, The tool shows parameters under Form Data section.
What is the difference ?
回答是:
you have not provided enough information how you use the send function, but I assume that you do not set mime type to specify you are sending form data
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");

the data sent are in this case encoded as you encode a query string
xhr.send("name=foo&value=bar");

otherwise it will not be interpreted as form data by Developer Tools. jquery does majority of work for you in this regard.
关键就是设置Content-type这个Header为application/x-www-form-urlencoded,实际上对于常规的HTML页面上的form的Content-type默认就是这个值。
相似的问题还发生在AngularJS的$http方法中,How can I make angular.js post data as form data instead of a request payload? 这个问题竟然有77个“顶”,看来遇到此问题的人还真不少。
注:这个问题里说jQuery的ajax方法是可以的,我今天遇到是不可以的,这个需要再验证一下。
当然解决的方法是一样的:
$http({ method: 'POST', url: url, data: xsrf, headers: {'Content-Type': 'application/x-www-form-urlencoded'} })

ArgularJS的$http方法还支持全局设置:
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";

还有人专门针对这个问题写了篇博客,分析了原因Make AngularJS $http service behave like jQuery.ajax()
The difference is in how jQuery and AngularJS serialize and transmit the data. Fundamentally, the problem lies with your server language of choice being unable to understand AngularJS’s transmission natively—that’s a darn shame because AngularJS is certainly not doing anything wrong. By default, jQuery transmits data using Content-Type: x-www-form-urlencoded and the familiar foo=bar&baz=moe serialization. AngularJS, however, transmits data using Content-Type: application/json and { “foo”: “bar”, “baz”: “moe” } JSON serialization, which unfortunately some Web server languages—notably PHP—do not unserialize natively.
当然文章了给出了另外的一种处理方案,还没细读,这个问题先记录在这里,以后再做些例子来详细研究一下。
另外,如果做Server端的API,默认支持哪种请求为好呢?本回答被提问者和网友采纳
第3个回答  推荐于2017-10-08
1、HTTP请求过程中,get请求:表单参数以name=value&name1=value1的形式附到url的后面;
2、post请求:表单参数是在请求体中,也是name=value&name1=value1的形式在请求体中。
POST表单请求提交时,使用的Content-Type是application/x-www-form-urlencoded,而使用原生AJAX的POST请求如果不指定请求头RequestHeader,默认使用的Content-Type是text/plain;charset=UTF-8。
在html中form的Content-type默认值:Content-type:application/x-www-form-urlencoded
如果使用ajax请求,在请求头中出现 request payload导致参数的方式改变了 ,那么解决办法就是:
headers: {'Content-Type':'application/x-www-form-urlencoded'}
或者使用ajax设置:
$.ajaxSetup({contentType: 'application/x-www-form-urlencoded'});
这样,问题就可以解决。
第4个回答  2018-07-31
  1、HTTP请求过程中,get请求:表单参数以name=value&name1=value1的形式附到url的后面;
2、post请求:表单参数是在请求体中,也是name=value&name1=value1的形式在请求体中。
POST表单请求提交时,使用的Content-Type是application/x-www-form-urlencoded,而使用原生AJAX的POST请求如果不指定请求头RequestHeader,默认使用的Content-Type是text/plain;charset=UTF-8。
在html中form的Content-type默认值:Content-type:application/x-www-form-urlencoded
如果使用ajax请求,在请求头中出现 request payload导致参数的方式改变了 ,那么解决办法就是:
headers: {'Content-Type':'application/x-www-form-urlencoded'}
或者使用ajax设置:
$.ajaxSetup({contentType: 'application/x-www-form-urlencoded'});
这样,问题就可以解决。