方法一
1.获取要传的信息
A.html页面,通过encodeURIComponent()方法进行编码
$.ajax({
type: 'POST',
url: baseLinkPath+'/Guest/login',
//contentType: 'application/html; charset=utf-8',//如果参数通过data: options传参,就去掉这行,不然后台接收不到参数
dataType: 'json',
cache: false,
data: formData,//或'loginPassword='+formData.password+'&loginName='+formData.username,
success: function (d) {
alert(d.data.user.userNickName);//超级管理员
if (d.code == '000000') {
var urlParameter="?token="+d.data.token+"&userName="+d.data.user.userName+"&userNickName="+encodeURIComponent(d.data.user.userNickName)+"&userId="+d.data.user.userId;
location.href = webLinkPath+formData.referer+urlParameter;
} else {
layer.msg('用户名或密码错误,请重试!',{icon:2,time:2000});
}
},
error: function (d) {
layer.msg('请求错误,请重试!',{icon:2,time:2000});
}
});
2.接授参数的页面
B.html
//接收多值
function manyValues() {
var url = window.location.search;
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
var key = new Array(strs.length);
var value = new Array(strs.length);
for (i = 0; i < strs.length; i++) {
key[i] = strs[i].split("=")[0]
value[i] = unescape(strs[i].split("=")[1]);
if("token"==key[i]){
$("#token").val(value[i]);
}
if("userName"==key[i]){
$("#userName").html(value[i]+' <i class="Hui-iconfont"></i>');
}
if("userNickName"==key[i]){
$("#userNickName").append(decodeURIComponent(value[i]));//解码,中文完美显示
}
}
}
}
方法二
1.将encodeURIComponent换成encodeURI方法
2.将decodeURIComponent换成decodeURI方法