ajax参数传递与后台接收

@

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

posted @ 2019-03-15 10:13  草木物语  阅读(31380)  评论(1编辑  收藏  举报