前几天在工作的时候,在和移动端做接口的时候发现,前端在传递参数的时候其中有+或者&时候,服务器获得串中+或者&都变成了空格,后台Java程序在解析的时候出错了!
具体代码如下:
$(function () {
var isPc = !(window.__detect.android||window.__detect.ios||window.__detect.phone),
moonCakeNums,
moonCakeIdentify= 0,
<span style="color:#ff0000;">token = userCookieControl.getCookie('authtoken'),
APP_ARGS = token?'&authtoken='+encodeURIComponent(token)+'&from=m':'',</span>
GUAGUA_SCHMA = 'guagua://guagua::5::13:1;18:2', //guagua://guagua::8::21:2
//退出的登录地址
logoutBackUrl = 'http://event.guagua.cn/event/zhongqiu/index.html';
//防止用户重复提交
function blockAjax(delay){
var $elem = $('body'),
isBlock = $elem.attr('data-block');
if(isBlock) return true;
$elem.attr('data-block', 'block');
setTimeout(function(){
$elem.removeAttr('data-block');
}, delay||1200 );
}
//登录弹框
function popupLoginDlg(){
if(isPc){
//末登录
guaguaLR._run('l');
} else {
window.location.href = GUAGUA_SCHMA;
}
}
function __checkLogin(){
var isLogin = checkLoginStatus();
if(!isLogin) popupLoginDlg();
return !!isLogin;
}
//ios首页隐藏兑换按钮
//if(window.__detect.ios) $('#J_exchange_btn').hide();
//首页兑换按钮
$('#J_exchange_btn').bind('click', function(e) {
e.preventDefault();
if(__checkLogin()) {
window.location.href = './duihuan.html';
}
});
其中从移动App的页面cookie获得用户的登录信息(authtoken)中的含有+和&符号,导致服务端获得authtoken时解析错误了,解决的方式就是对authtoken进行URL转义。代码如下:
APP_ARGS = token?'&authtoken='+encodeURIComponent(token)+'&from=m':'',
这样服务器就能够获得正常的authtoken,并解析得到其中用户信息!