分享

后端无法解析前端发送到request payload类型数据

 ansatsing 2018-04-08

前端页面截图:

后端代码截图:

报错界面:

why???下面会解释:

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

AJAX Post请求中常用的两种传参数的形式:form data 和 request payload

Form data

get请求的时候,我们的参数直接反映在url里面,形式为key1=value1&key2=value2形式,比如:

1

http://news.baidu.com/ns?word=NBA&tn=news&from=news&cl=2&rn=20&ct=1

如果是post请求,那么表单参数是在请求体中,也是以key1=value1&key2=value2的形式在请求体中。通过chrome的开发者工具可以看到如下:

123456789101112131415161718192021222324252627

RequestURL:http://127.0.0.1:8080/test/test.doRequest Method:POSTStatus Code:200 OK Request HeadersAccept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Accept-Encoding:gzip,deflate,sdchAccept-Language:zh-CN,zh;q=0.8,en;q=0.6AlexaToolbar-ALX_NS_PH:AlexaToolbar/alxg-3.2Cache-Control:max-age=0Connection:keep-aliveContent-Length:25Content-Type:application/x-www-form-urlencodedCookie:JSESSIONID=74AC93F9F572980B6FC10474CD8EDD8DHost:127.0.0.1:8080Origin:http://127.0.0.1:8080Referer:http://127.0.0.1:8080/test/index.jspUser-Agent:Mozilla/5.0 (Windows NT 6.1)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.149 Safari/537.36 Form Dataname:mikanaddress:street Response HeadersContent-Length:2Date:Sun, 11 May 2014 11:05:33 GMTServer:Apache-Coyote/1.1

这里要注意post请求的Content-Type为application/x-www-form-urlencoded(默认的),参数是在请求体中,即上面请求中的Form Data。

前端:

12

xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");xhr.send("name=foo&value=bar");

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

12345678910

/** * 获取httpRequest的参数 *   * @param request * @param name * @return */protected String getParameterValue(HttpServletRequest request, String name) {    return StringUtils.trimToEmpty(request.getParameter(name));}

Request payload

如果使用原生AJAX POST请求的话,那么请求在chrome的开发者工具的表现如下,主要是参数在

123456789101112131415161718192021222324252627282930

Remote Address:192.168.234.240:80Request URL:http://tuanbeta3.XXX.com/qimage/upload.htmRequest Method:POSTStatus Code:200 OKRequest HeadersAccept:application/json, text/javascript, */*; q=0.01Accept-Encoding:gzip,deflate,sdchAccept-Language:zh-CN,zh;q=0.8,en;q=0.6Connection:keep-aliveContent-Length:151Content-Type:application/json;charset=UTF-8Cookie:JSESSIONID=E08388788943A651924CA0A10C7ACAD0Host:tuanbeta3.XXX.comOrigin:http://tuanbeta3.XXX.comReferer:http://tuanbeta3.XXX.com/qimage/customerlist.htm?menu=19User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36X-Requested-With:XMLHttpRequestRequest Payload[{widthEncode:NNNcaXN, heightEncode:NNNN5NN, displayUrl:201409/03/66I5P266rtT86oKq6,…}]Response HeadersConnection:keep-aliveContent-Encoding:gzipContent-Type:application/json;charset=UTF-8Date:Thu, 04 Sep 2014 06:49:44 GMTServer:nginx/1.4.7Transfer-Encoding:chunkedVary:Accept-Encoding

注意请求的Content-Type为application/json;charset=UTF-8,而请求表单参数在Request Payload中。

后端获取(这里使用org.apache.commons.io.):

12345678910

/** * 从 request 获取 payload 数据 * * @param request * @return * @throws IOException */private String getRequestPayload(HttpServletRequest request) throws IOException {    return IOUtils.toString(request.getReader());}

二者区别

参考:http:///questions/10494574/what-is-the-difference-between-form-data-and-request-payload

if a request (typically POST) has Content-type header set to application/x-www-form-urlencoded the body is expected to be in the form of a standard querystring with url-encoded key=value pairs joined by &. Form data section then shows the key-value parameters (when viewed parsed). This way was much more common in past because it is a default for HTML forms.

other cases are shown in Request payload section (and nowadays parsed for readability as well for common formats like JSON).

如果请求的Content-Type设置为application/x-www-form-urlencoded,那么这个Post请求被认为是HTTP POST表单请求,参数出现在

其他情况如使用原生AJAX的POST请求如果不指定请求头Request Header,默认使用的Content-Type是text/plain;charset=UTF-8,参数出现在Request payload块。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多