网站空间租赁,自建网站网址,外贸建站模板价格,公司注册一站式ajax参数传递与后台接收Servlet中读取http参数的方法Enumeration getParameterNames() 返回一个 String 对象的枚举#xff0c;包含在该请求中包含的参数的名称String getParameter(String name) 以字符串形式返回请求参数的值#xff0c;或者如果参数不存在则返回 null。Str…ajax参数传递与后台接收Servlet中读取http参数的方法Enumeration getParameterNames() 返回一个 String 对象的枚举包含在该请求中包含的参数的名称String getParameter(String name) 以字符串形式返回请求参数的值或者如果参数不存在则返回 null。String getQueryString() 返回包含在路径后的请求 URL 中的查询字符串。String[] getParameterValues(String name) 返回一个字符串对象的数组包含所有给定的请求参数的值如果参数不存在则返回 null。ServletInputStream getInputStream() 使用 ServletInputStream以二进制数据形式检索请求的主体。ajax默认contentType为application/x-www-form-urlencoded使用默认contentType参数追加到url后传递$.ajax({url: http://localhost:8082/boot/request/parameter?nameaaaparaBbbb,contentType: application/x-www-form-urlencoded;charsetutf-8,success: function(json){console.log(json);}});//请求中包含的参数的名称Enumeration parameterNames request.getParameterNames();while (parameterNames.hasMoreElements()) {String s parameterNames.nextElement();System.out.println(getParameterNames: s);String paraA request.getParameter(s);System.out.println(getParameter: paraA);}// 返回包含在路径后的请求 URL 中的查询字符串String queryString request.getQueryString();System.out.println(getQueryString: queryString);使用默认contentType参数放到data中传递$.ajax({url: http://localhost:8082/boot/request/parameter,contentType: application/x-www-form-urlencoded;charsetutf-8,data: {name: aaa, paraB:bbb},success: function(json){console.log(json);}});//请求中包含的参数的名称Enumeration parameterNames request.getParameterNames();while (parameterNames.hasMoreElements()) {String s parameterNames.nextElement();System.out.println(getParameterNames: s);String paraA request.getParameter(s);System.out.println(getParameter: paraA);}使用默认contentTypedata中传递数组$.ajax({url: http://localhost:8082/boot/request/parameter,type: post,contentType: application/x-www-form-urlencoded;charsetutf-8,data: {foo: [bar1, bar2]},success: function (json) {console.log(json);}});// 返回一个字符串对象的数组String[] parameterValues request.getParameterValues(foo[]);if (parameterValues ! null) {for (String parameterValue : parameterValues) {System.out.println(getParameterValues: parameterValue);}}使用contentType为application/json在data中传递复杂参数$.ajax({url: http://localhost:8082/boot/request/parameter,type: post,contentType: application/json;charsetutf-8,data: JSON.stringify({name: aaa, foo: [bar1, bar2]}),success: function (json) {console.log(json);}});// 以二进制数据形式检索请求的主体ServletInputStream inputStream request.getInputStream();BufferedReader br new BufferedReader(new InputStreamReader(inputStream));String line ;StringBuilder sb new StringBuilder();while ((line br.readLine()) ! null) {sb.append(line);}System.out.println(getInputStream: sb);使用contentType为application/json在data中传递复杂参数并使用springmvc接收$.ajax({url: http://localhost:8082/boot/request2/requestBody,type: post,contentType: application/json;charsetutf-8,data: JSON.stringify({id: 111, name: aaa, foo: [bar1, bar2]}),success: function (json) {console.log(json);}});RequestMapping(value /requestBody)public void RequestBody(RequestBody User user) throws IOException {System.out.println(user.toString());}url追加参数与data中放json同时使用$.ajax({// url: http://localhost:8082/boot/request/parameter?userNameaaa,url: http://localhost:8082/boot/request2/parm?userNameaaa,type: post,contentType: application/json;charsetutf-8,data: JSON.stringify({id: 111, name: aaa, foo: [bar1, bar2]}),async:false,success: function (json) {console.log(json);}});//请求中包含的参数的名称Enumeration parameterNames request.getParameterNames();while (parameterNames.hasMoreElements()) {String s parameterNames.nextElement();System.out.println(getParameterNames: s);String paraA request.getParameter(s);System.out.println(getParameter: paraA);}// 以二进制数据形式检索请求的主体ServletInputStream inputStream request.getInputStream();BufferedReader br new BufferedReader(new InputStreamReader(inputStream));String line ;StringBuilder sb new StringBuilder();while ((line br.readLine()) ! null) {sb.append(line);}System.out.println(getInputStream: sb);RequestMapping(value /parm)public void parm(RequestParam String userName, RequestBody User user) throws IOException {System.out.println(userName: userName);System.out.println(user.toString());}