@

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?name=aaa&paraB=bbb",
contentType: "application/x-www-form-urlencoded;charset=utf-8",
success: function(json){
console.log(json);
}
});
//请求中包含的参数的名称
Enumeration<String> 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;charset=utf-8",
data: {name: "aaa", paraB:"bbb"},
success: function(json){
console.log(json);
}
});
//请求中包含的参数的名称
Enumeration<String> 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);
}

使用默认contentType,data中传递数组

$.ajax({
url: "http://localhost:8082/boot/request/parameter",
type: "post",
contentType: "application/x-www-form-urlencoded;charset=utf-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;charset=utf-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;charset=utf-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?userName=aaa",
url: "http://localhost:8082/boot/request2/parm?userName=aaa",
type: "post",
contentType: "application/json;charset=utf-8",
data: JSON.stringify({id: 111, name: "aaa", foo: ["bar1", "bar2"]}),
async:false,
success: function (json) {
console.log(json);
}
});
//请求中包含的参数的名称
Enumeration<String> 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());
}

参考文章:

https://www.w3cschool.cn/servlet/servlet-client-request.html

https://blog.csdn.net/qq_34129814/article/details/72604347

ajax参数传递与后台接收的更多相关文章

  1. ajax传递数组后台接收不到值的问题

    背景: JQGrid需要进行批量删除操作传给后台的是数组,结果后台接收不到值. 后台语言:java 原因: ajax传递参数时,traditional 默认为false,JQuery会深度序列化参数对 ...

  2. ajax 发送json 后台接收 遍历保存进数据库

    前台怎么拿参数的我就不管了我也不会 反正用这个ajax没错 ajax 代码   一定要写明http请求类型  { contentType:"application/x-www-form-ur ...

  3. 原生ajax提交php后台接收不到问题

    var xmlHttp; if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); ...

  4. 【JS】中ajax的URL中包含中文,后台接收乱码

    [问题]ajax提交get请求,url中参数包含中文,后台接收到显示乱码. [解决方案]前台: function getSiteInfoByName(siteName){ var res; $.aja ...

  5. Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法

    之前写过一篇记录文章,写的是将一个比较复杂的数据结构在前台组合起来后传递到后台. 当时并不太了解@RequestBody,也并没有使用js提供的JSON.stringify()方法 所有都是自己写的, ...

  6. ajax 传递数组类型参数后台接收不到的问题

    在做排序功能的时候需要将一个数组的数据传递到后台,(当时怎么没用json,如果用json就没有那么多的事情了),数据提交采用ajax! 先看代码 js: submitbtn: function () ...

  7. SpringMVC后台接收list类型的数据的实现方式

    一.背景 最近在做一些东西的时候,遇到一个需要Springmvc后台接收list类型数据的需求,几经辗转才完美解决了这个问题,今天记下来方便以后使用,也分享给需要的小伙伴们~ 二.实现方式 1.实现方 ...

  8. angular的$http.post()提交数据到Java后台接收不到参数值问题的解决方法

    本文地址:http://www.cnblogs.com/jying/p/6733408.html   转载请注明出处: 写此文的背景:在工作学习使用angular的$http.post()提交数据时, ...

  9. ASP.NET前台table通过Ajax获取绑定后台查询的json数据

    上一篇<ASP.NET前台html页面AJAX提交数据后台ashx页面接收数据>写了前台提交数据后台保存到数据库,数据处理以后用户肯定要查询.接下来就写一个前台table通过ajax  J ...

随机推荐

  1. 使用cobbler工具实现centos 6,7系统的自动化安装

    vmware里面准备两台虚拟机,一台用于安装cobbler服务器,另一台当作测试机使用,cobbler服务器需要两块网卡,一块需要连接外网,需要使用epel源.测试机使用一块仅主机的模式的网卡,注意要 ...

  2. vue鼠标悬停事件

    v-bind:title="message" <!DOCTYPE html> <html lang="en"> <head> ...

  3. 升级WIN10 (9879)后IE无响应的解决办法

    身为程序猿,当然有了新系统就要尝尝鲜,有WIN8时,哥是朋友圈第一个用的,有WIN8.1时哥也是第一个升级的. 现在WIN10来了,当然也得赶紧尝尝鲜.直接下载了 9879版的预览版本安装. 要说WI ...

  4. vue 项目中引用百度地图

    新建map.js export const BaiduMap = { init: function() { const BMapURL = 'https://api.map.baidu.com/api ...

  5. js实现浏览器调用电脑的摄像头拍照

    <!DOCTYPE html> <html lang="en"> <head> <style> * { margin: ; padd ...

  6. Android 之文件夹排序

    按文件名排序 /** * 按文件名排序 * @param filePath */ public static ArrayList<String> orderByName(String fi ...

  7. 织梦CMS增加复制文档功能

    打开后台目录(/dede)下archives_do.php约430行下添加: /*----------------------------- //复制文档 ---------------------- ...

  8. MyDAL - .Where() & .And() & .Or() 使用

    索引: 目录索引 一.API 列表 1.Where .Where(Func<M, bool> func) 如: .Where( it => (it.Prop1>=条件1 &am ...

  9. 查看CPU使用率

    rem 如果wmi服务(服务名为Winmgmt)坏掉了,需要到system32\webm目录下执行如下注释的命令 rem for %i in (*.dll) do RegSvr32 -s %i rem ...

  10. vue typescript ui库

    https://blog.csdn.net/phj_88/article/details/81302043 vuetifyjs