ajax前端传map数组,一文搞定RequestParam注解传参,map 数组 文件(二)

上篇讲了后台用@RequestParam来接各种参数,这篇就讲下用ajax作为前端往后台请求数据。

回顾ajax请求

常用的几个参数

url

请求地址

data

发送到服务器的数据。将自动转换为请求字符串格式。GET 请求中将附加在 URL 后。查看 processData 选项说明以禁止此自动转换。必须为 Key/Value 格式。如果为数组,jQuery 将自动为不同值对应同一个名称。如 {foo:["bar1", "bar2"]} 转换为 '&foo=bar1&foo=bar2'。

dataType

预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断,比如 XML MIME 类型就被识别为 XML。

"xml": 返回 XML 文档,可用 jQuery 处理。

"html": 返回纯文本 HTML 信息;包含的 script 标签会在插入 dom 时执行。

"script": 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了 "cache" 参数。注意:在远程请求时(不在同一个域下),所有 POST 请求都将转为 GET 请求。(因为将使用 DOM 的 script标签来加载)

"json": 返回 JSON 数据 。

"jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。

"text": 返回纯文本字符串

type

请求方式 ("POST" 或 "GET"), 默认为 "GET"。注意:其它 HTTP 请求方法,如 PUT 和 DELETE 也可以使用,但仅部分浏览器支持。

async

默认值: true。默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。

注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。

error

默认值: 自动判断 (xml 或 html)。请求失败时调用此函数。

有以下三个参数:XMLHttpRequest 对象、错误信息、(可选)捕获的异常对象。

如果发生了错误,错误信息(第二个参数)除了得到 null 之外,还可能是 "timeout", "error", "notmodified" 和 "parsererror"。

这是一个 Ajax 事件。

success

类型:Function

请求成功后的回调函数。

参数:由服务器返回,并根据 dataType 参数进行处理后的数据;描述状态的字符串。

这是一个 Ajax 事件。

组装各类请求

为了方便做了个简单的含有各类请求按钮的页面

bdce57692569

image.png

单个参数

$("#simple").click(function () {

$.ajax({

url: 'http://localhost:8888/param/simple',

data: {'id': "1"},

contentType: 'application/x-www-form-urlencoded',

dataType: 'text',

success: function (data) {

alert(data);

}

})

});

bdce57692569

image.png

map 参数

$("#map").click(function () {

$.ajax({

url: 'http://localhost:8888/param/map',

data: {'name': '王', 'sex': '男', 'age': '18'},

contentType: 'application/x-www-form-urlencoded',

dataType: 'text',

success: function (data) {

alert(data);

}

})

});

bdce57692569

image.png

MultiValueMap 一键多值参数

$("#mutilValueMap").click(function () {

$.ajax({

url: 'http://localhost:8888/param/multiMap',

data: {name: ['王', '张', '李']},

contentType: 'application/x-www-form-urlencoded',

dataType: 'text',

success: function (data) {

alert(data);

}

})

});

注意:这里的key name经过ajax处理变成了name[] ,感觉并不友好,有知道的小伙伴请评论指教,如何去除?

bdce57692569

image.png

说明这跟直接使用http://localhost:8888/param/multiMap?name=王&name=张&name=李 还是有区别的,使用直接url构造的方式key并没有变化,跟我们需要的预期一致

$("#mutilValueMap").click(function () {

$.ajax({

url: 'http://localhost:8888/param/multiMap?name=王&name=张&name=李',

// data: {name: ['王', '张', '李']},

contentType: 'application/x-www-form-urlencoded',

dataType: 'text',

success: function (data) {

alert(data);

}

})

});

bdce57692569

image.png

数组传参

$("#strings").click(function () {

var names = [];

names.push('王');

names.push('张');

names.push('李');

$.ajax({

url: 'http://localhost:8888/param/array',

data: {"names":names.join()},

contentType: 'application/x-www-form-urlencoded',

dataType: 'text',

success: function (data) {

alert(data);

}

})

});

bdce57692569

image.png

html完整源代码

测试spring @RequestParamc传参

单个参数传参

map参数传参

MutilValueMap参数传参

String数组参数传参

$(document).ready(function () {

$("#simple").click(function () {

$.ajax({

url: 'http://localhost:8888/param/simple',

data: {'id': "1"},

contentType: 'application/x-www-form-urlencoded',

dataType: 'text',

success: function (data) {

alert(data);

}

})

});

$("#map").click(function () {

$.ajax({

url: 'http://localhost:8888/param/map',

data: {'name': '王', 'sex': '男', 'age': '18'},

contentType: 'application/x-www-form-urlencoded',

dataType: 'text',

success: function (data) {

alert(data);

}

})

});

$("#mutilValueMap").click(function () {

$.ajax({

url: 'http://localhost:8888/param/multiMap',

data: {name: ['王', '张', '李']},

contentType: 'application/x-www-form-urlencoded',

dataType: 'text',

success: function (data) {

alert(data);

}

})

});

$("#strings").click(function () {

var names = [];

names.push('王');

names.push('张');

names.push('李');

$.ajax({

url: 'http://localhost:8888/param/array',

data: {"names":names.join()},

contentType: 'application/x-www-form-urlencoded',

dataType: 'text',

success: function (data) {

alert(data);

}

})

});

});

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值