AJAX设置content-type为:application/json提交参数后台接收参数的问题

我一直做java的,对前端说不上有多了解,以前遇到的前端的开发人员,包括我自己再写ajax的时候基本上都是以下写法:

$('#send').click(function(){
         $.ajax({
			 headers:{
				Authorization : "Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJVa1p1dEsxQm1PX2p3V09WVDJlbk16Z2piaHljZm1Ld0RiRkV1Q1RhdjY0In0.eyJqdGkiOiI1YzA2ZTk3NS1hNDRjLTRhMzctYmMyNy1hNDlhYzlhODc2MDMiLCJleHAiOjE1NDE1OTExNzMsIm5iZiI6MCwiaWF0IjoxNTQxNTkwMjczLCJpc3MiOiJodHRwczovL2Rldi54dWViYXN0dWR5LmNvbS9hdXRoL3JlYWxtcy9zYXRvIiwiYXVkIjoic2FsZXMtY2xpZW50Iiwic3ViIjoiN2Q0ODIxMDgtM2UzNi00OThmLWEyODAtYjE0YjhmMGJlN2ZkIiwidHlwIjoiQmVhcmVyIiwiYXpwIjoic2FsZXMtY2xpZW50Iiwibm9uY2UiOiJlYzFhNGIxMy04ZWZhLTQ1MTQtODkyZC1mNmU0ODkyZmNmNmYiLCJhdXRoX3RpbWUiOjE1NDE1OTAyNTMsInNlc3Npb25fc3RhdGUiOiIwMDEyNmUzYS1jODcxLTRjZWYtYTg4Ni1hZDY0YTc1YWMyY2MiLCJhY3IiOiIwIiwiY2xpZW50X3Nlc3Npb24iOiI0YTA2NTY0YS04Yzg4LTRkYjgtOTNlOS04OWI4MTBiY2FjODIiLCJhbGxvd2VkLW9yaWdpbnMiOlsiaHR0cDovL2xvY2FsaG9zdDo4MDgwIiwiaHR0cDovLzE5Mi4xNjguMC4yIl0sInJlYWxtX2FjY2VzcyI6eyJyb2xlcyI6WyJTYWxlcyIsIlRlYWNoZXJzIiwiU2FsZUFkbWluIiwiVGVhY2hlckFkbWluIiwidW1hX2F1dGhvcml6YXRpb24iXX0sInJlc291cmNlX2FjY2VzcyI6eyJzYXRvLW5vdGlmaWNhdGlvbiI6eyJyb2xlcyI6WyJUZWFjaGVycyIsIlNhbGVzIiwiU2FsZUFkbWluIiwiVGVhY2hlckFkbWluIl19LCJzYWxlcy1jcm0iOnsicm9sZXMiOlsiU2FsZXMiLCJTYWxlQWRtaW4iXX0sInNhbGVzLWxlYWRzLWh0dHAiOnsicm9sZXMiOlsiU2FsZXMiXX0sImFjY291bnQiOnsicm9sZXMiOlsibWFuYWdlLWFjY291bnQiLCJ2aWV3LXByb2ZpbGUiXX19LCJuYW1lIjoi6ZSA5ZSuIDAxIiwicHJlZmVycmVkX3VzZXJuYW1lIjoic2FsZXMwMSIsImdpdmVuX25hbWUiOiLplIDllK4iLCJmYW1pbHlfbmFtZSI6IjAxIn0.TyqgUCost9sDy-tE8KAPW_3tBbTnzm6xfgIY2c1FhEYR3-mfLLbh0LLdkqDJEMs9AIvElOyU1jf9YA4laA6imaNqAshPvrRhxh4wIqJ9cCTNJjrou98gxkccRSSaFX19VctxLxRGk1wC_ZlIqdg9rgklVJE1rpDpzOnm1WF-skSwoQI7UZINEeizVZC21T4eIvDLoWUU6Qp9LKW3kcahU0k44S2v1k-lMJzE8dD4rQco7Z1vswXvYOxpk5g-RPKiREo1je-ZGyZLXZiJPdLwIm7mGp1x6qOxIUnbrMZk24VwS48hBhbbKeahBS-lvfEck0vLls3EVL0JMx9gre3Jbg"
			 },
             type: "POST",
             url: "https://dev.xuebastudy.com/crm-cti/userDetail",
             data: {
					"id":"4",
					"customerNumber":"13315863006",
					"customerId":"test",
					"salesId":4
				   },
             dataType: "json",
             success: function(data){
				alert(JSON.stringify(data))
             }
         });
    });

所以后台在接收参数的时候也习惯了 request.getParameter(),或者使用@RequestParam的注解的方式。

这样是没有问题的,因为这种方式提交的时候他的请求头如下

但是新公司的前端,喜欢使用使用$.ajax(url,data,callback)的方式发起ajax,并且设定了content-type为 application/json。但是在Post的时候,我后台居然接受不到参数,瞬间一脸懵逼。

使用POSTMan模拟下情况如下

查看ajax的请求代码如下:

 

对于这种方式,如果没有使用框架的情况下,只能采用传统的流的方式接收:


    private String getXmlString(HttpServletRequest request) {
        BufferedReader reader = null;
        String line = "";
        String xmlString = null;
        try {
            reader = request.getReader();
            StringBuffer inputString = new StringBuffer();

            while ((line = reader.readLine()) != null) {
                inputString.append(line);
            }
            xmlString = inputString.toString();
        } catch (Exception e) {
//            throw new YppException(PayException.xmlParseError);
        }
        return xmlString;
    }

但是在spring boot的中可以使用@RequestBody加上对象接收

@RequestBody ParamUserDetail userDetail

当然如果不愿意构造对象的时候可以使用Map<String,String>来接收,对于少量参数建议使用这种方式

@RequestBody Map<String,String> params

 

展开阅读全文

没有更多推荐了,返回首页