encodeURIComponent() 函数的使用
说明:encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。
维护项目中,遇到一个登录的问题:(用户的loginName为33195221,密码为147258369+),在密码正确的情况下登录,显示密码错误。
于是翻看了代码,看到了登录请求的代码为这样的:
$("#login").click(function() {
var userName = $("#userName").val();
var password = $("#password").val(); var url = basePath + '/user/user_login.do';
url = url + '?user.userName=' + userName + '&user.password=' + password;
$.ajax({
url : url,
dataType : 'json',
type : "post",
success : function(data) {
if (data.resultStatus == 'ok') {
window.location.href='index.html';
}else{
alert('登录失败');
}
},
error : function() {
alert("未能连接到服务器!");
}
}); });
访问的url:
看着没问题,但是传到后台后,明显后面的特殊字符“+”号变成了空格,如下图:
所以,登录的时候就显示密码错误。
解决办法:
像这种url中带有特殊字符的情况下,就用encodeURIComponent() 函数,把要编码的字符串传进去,比如,刚开始的js代码中的url关于密码的那块,可以这样改:
url = url + '?user.userName=' + userName + '&user.password=' + encodeURIComponent(password);
这样就不会把传过去的特使字符“+”变为空格了。 注:encodeURIComponent不编码字符有71个:!, ',(,),*,-,.,_,~,0-9,a-z,A-Z
encodeURIComponent() 函数的使用的更多相关文章
- JavaScript escape encodeURI encodeURIComponent() 函数
总结一下: 1.encodeURI(),和encodeURIComponent()是对字符进行编码. 2.decodeURI(),和decodeURIComponent()是对相应编码过的字符进行解码 ...
- c#实现Javascript的encodeURIComponent()函数
原文 c#实现Javascript的encodeURIComponent()函数 国内外各搜索引擎,均用JavaScript的encodeURIComponent()函数对搜索关键字进行编码,终于找 ...
- escape,encodeURI,encodeURIComponent函数比较
escape,encodeURI,encodeURIComponent函数比较 js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数 ...
- encodeURIComponent() 函数
https://baike.baidu.com/item/encodeURIComponent() 函数/7418815?fr=aladdin encodeURIComponent() 函数[1] 作 ...
- JS中encodeURIComponent函数用php解码的代码
JS中encodeURIComponent函数给中文编码后,如何用php解码?? 前提:编码前的中文可能是gbk,gb2312,utf-8等. 复制代码 代码如下: urldecode() iconv ...
- js中的encodeURIComponent()函数
encodeURIComponent() 函数可把字符串作为 URI 组件进行编码. $scope.linktotheme = function () { if ($scope.curthemeid ...
- JS encodeURIComponent函数
为了避免歧义,可以用JS 的encodeURIComponent函数 将有歧义的字符(?+=等)转换成对应的ASCII编码 for(var i=0;i<whichform.elements.l ...
- encodeURIComponent() 函数可把字符串作为 URI 组件进行编码
语法 encodeURIComponent(URIstring) 参数 描述 URIstring 必需.一个字符串,含有 URI 组件或其他要编码的文本. 返回值 URIstring 的副本,其中的某 ...
- JavaScript URL编码转换函数 encodeURIComponent()
encodeURIComponent()定义和用法 encodeURIComponent() 函数可把字符串作为 URI 组件进行编码. 语法:encodeURIComponent(URIstring ...
随机推荐
- 《条目二十九:对于逐个字符的输入请考虑istreambuf_iterator》
<条目二十九:对于逐个字符的输入请考虑istreambuf_iterator> 1.使用: ifstream inputfile("xxxx"); string fil ...
- ASP.NET:Application,Session,Cookie,ViewState和Cache之间的区别(转)
在ASP.NET中,有很多种保存信息的对象.例如:Application,Session,Cookie,ViewState和Cache等,那么它们有什么区别呢?每一种对象应用的环境是什么? 为了更清楚 ...
- JAVA编程思想第一章——对象导论
- Web安全学习笔记——SQL注入
一.MySQL注入 1. 常用信息查询 常用信息: 当前数据库名称:database() 当前用户:user() current_user() system_user() 当前数据库版本号:@@ver ...
- PowerDesigner16 生成的备注脚本,在sql server 2008 中报“对象名 'sysproperties' 无效”的错误的解决办法
主要是在建模时我们对表.列增加了些说明注释,而Sql2005之后系统表sysproperties已废弃删除而改用sys.extended_properties所致. 1.修改Table TableCo ...
- Asp.NET MVC 拍卖网站,拆解【1】预览与目录
本人最近带创业团队基本做完了一个艺术品拍卖的外包项目,分为网站前台(asp.net mvc5),网站管理员管理的后台使用的9900端口(asp.net mvc5),监听拍卖状态的windows服务,为 ...
- C#面试:委托
面试常见题: 1.委托是什么?★☆ 2.为什么需要委托?★☆ 3.委托能用来做什么?★☆ 4.如何自定义委托★☆ 5..NET默认的委托类型有哪几种?★☆ 6.怎样使用委托?★★★ 7.多播委托是什么 ...
- media(适配)
媒体类型 1.all 所有媒体 2.braille 盲文触觉设备 3.embossed 盲文打印机 4.print 手持设备 5.projection 打印预览 ...
- SecureCRTPortable的安装和使用(图文详解)
不多说,直接上干货! 玩玩这个远程连接软件,是个绿色软件. 别人已经做好了的. 解压之后, 下面,软件展示下, 这会默认去打开, 为了,方便,使用,放到桌面,作为快捷方式 成功 欢迎大家,加入我的微信 ...
- JavaScript数据结构-11.散列
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...