js通过url向后台传List对象遇到问题,异常提示:Invalid character found in the request target [/erecord/recommendation/examineBatchNew?data=[{]. The valid characters are defined in RFC 7230 and RFC 3986
异常信息大概的意思是请求头中包含了 RFC 7230 and RFC 3986规范中定义的非法字符。
一、出现问题代码片段
1、js片段:row是从bootstrapTable表格选中的对象,并通过JSON.stringify(row)转化为json字符串data,然后通过url传递到后台方法
function examineBatchNew() {
var row = $("#bootstrap-table").bootstrapTable('getSelections');
if ($.common.isEmpty(row)) {
$.modal.alertWarning("请至少选择一条记录");
return;
} else {
var row = $("#bootstrap-table").bootstrapTable('getSelections');
var data= JSON.stringify(row);
var url = prefix + '/examineBatchNew?data='+data;
openexamine("批量通过原因", url);
}
}
function openexamine(title, url, width, height, callback) {
// 如果是移动端,就使用自适应大小弹窗
if ($.common.isMobile()) {
width = 'auto';
height = 'auto';
}
if ($.common.isEmpty(title)) {
title = false;
}
if ($.common.isEmpty(url)) {
url = "/404.html";
}
if ($.common.isEmpty(width)) {
width = 1000;
}
if ($.common.isEmpty(height)) {
height = 600;
}
if ($.common.isEmpty(callback)) {
callback = function (index, layero) {
var iframeWin = layero.find('iframe')[0];
iframeWin.contentWindow.submitHandler(index, layero);
}
}
top.layer.open({
type: 2,
area: [width + 'px', height + 'px'],
fix: false,
//不固定
maxmin: true,
shade: 0.3,
title: title,
content: url,
// 弹层外区域关闭
shadeClose: true,
yes: callback,
end:function () {
$.table.refresh('bootstrap-table');
},
cancel: function (index) {
return true;
}
});
}
2、controller:
@RequestMapping("/examineBatchNew")
public String examineBatchNew(String data, ModelMap mmap){
mmap.put("data", data);
return sjPrefix + "/examineBatch";
}
3、js执行报错,提示:Invalid character found in the request target [/erecord/recommendation/examineBatchNew?data=[{]. The valid characters are defined in RFC 7230 and RFC 3986,并没有进入后台断点,查看js传递的参数信息data为
报错原因:RFC3986文档规定,Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符,该参数包含了多项无法识别的字符
二、解决方案
在js使用encodeURI()对数据进行重新编码,在后台使用URLDecoder.decode()进行解码即可
1、js片段:
2、controller: