毕业设计《项目管理》总结06之ajax的初步使用经验

时间:2024-01-17 21:25:20

1、ajax页面时不能实现下载功能,因为后台下载功能返回的是一个流,而ajax得到后台的数据只能是字符串或字符,所以实现的方法可以:

例如:用js生成一个form,用这个form提交参数,并返回“流”类型的数据。在实现过程中,页面也没有进行刷新。

var form=$("<form>");//定义一个form表单
form.attr("style","display:none");
form.attr("target","");
form.attr("method","post");
form.attr("action","exportData");
var input1=$("<input>");
input1.attr("type","hidden");
input1.attr("name","exportData");
input1.attr("value",(new Date()).getMilliseconds());
$("body").append(form);//将表单放置在web中
form.append(input1); form.submit();//表单提交

2、在页面内有多文件上传时,存在一个链接来进行文件下载时,可以使用第1个方法实现。

3、在页面内有多文件上传时,存在一个链接来进行文件删除时,使用ajax向后台传递参数,参数封装在data中,同时将contentType设置为application/x-www-form-urlencoded;charset=utf-8,这样后台便可以得到参数。

4、ajax传递参数方法有:

①使用默认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.cnblogs.com/ooo0/p/10535278.html