ajax方法中的参数:
contentType:发送至服务器时内容的编码类型,
一般默认:
application/x-www-form-urlencoded
(适应大多数的场合)
dataType:预期服务器返回的数据类型
有时候前台ajax向后台传数据的时候,能够进到后台,但是后台显示接受的参数为
null
,如果ajax没有问题,这个时候就要考虑后台在接收参数的时候使用的是什么注解。(@RequestParam还是@RequestBody)
这样就可以解决后台接不到参数的问题啦!!!!
public HttpResponseEntity selectByCollege(@RequestParam Map<String,Object> collegeName) {
使用
@RequestParam
时
:
(推荐这个)
contentType:"
application/x-www-form-urlencoded
"
data:{'college',college}
使用
@RequestParam
,可以不用写contentType,
application/x-www-form-urlencoded是contentType的默认值
使用
@RequestBody
时:
接收的是json字符串格式的数据,
需要将
contentType写成:'
application/json
',
data:Json.Stringly(da)(将对象变成字符串)
let da = {'state':state};
$.ajax({
type: "POST",
url: httpUrl + "/insertInfo",
dataType: 'json',
data: JSON.stringify(da),
contentType: "application/json",
success: function (result) {
console.log(result);
$('#ModalInfo').modal('hide');
},
error: function () {
console.log('错误')
}
})
版权声明:本文为博主原创文章,未经博主允许不得转载。