说明: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() 函数的使用的更多相关文章

  1. JavaScript escape encodeURI encodeURIComponent() 函数

    总结一下: 1.encodeURI(),和encodeURIComponent()是对字符进行编码. 2.decodeURI(),和decodeURIComponent()是对相应编码过的字符进行解码 ...

  2. c#实现Javascript的encodeURIComponent()函数

    原文  c#实现Javascript的encodeURIComponent()函数 国内外各搜索引擎,均用JavaScript的encodeURIComponent()函数对搜索关键字进行编码,终于找 ...

  3. escape,encodeURI,encodeURIComponent函数比较

    escape,encodeURI,encodeURIComponent函数比较 js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数 ...

  4. encodeURIComponent() 函数

    https://baike.baidu.com/item/encodeURIComponent() 函数/7418815?fr=aladdin encodeURIComponent() 函数[1] 作 ...

  5. JS中encodeURIComponent函数用php解码的代码

    JS中encodeURIComponent函数给中文编码后,如何用php解码?? 前提:编码前的中文可能是gbk,gb2312,utf-8等. 复制代码 代码如下: urldecode() iconv ...

  6. js中的encodeURIComponent()函数

    encodeURIComponent() 函数可把字符串作为 URI 组件进行编码. $scope.linktotheme = function () { if ($scope.curthemeid ...

  7. JS encodeURIComponent函数

    为了避免歧义,可以用JS 的encodeURIComponent函数  将有歧义的字符(?+=等)转换成对应的ASCII编码 for(var i=0;i<whichform.elements.l ...

  8. encodeURIComponent() 函数可把字符串作为 URI 组件进行编码

    语法 encodeURIComponent(URIstring) 参数 描述 URIstring 必需.一个字符串,含有 URI 组件或其他要编码的文本. 返回值 URIstring 的副本,其中的某 ...

  9. JavaScript URL编码转换函数 encodeURIComponent()

    encodeURIComponent()定义和用法 encodeURIComponent() 函数可把字符串作为 URI 组件进行编码. 语法:encodeURIComponent(URIstring ...

随机推荐

  1. Mysql update from

    UPDATE tab1   a INNER JOIN tab_game_version as b ON a.id=b.id SET a.advert_data=0 where a.advert_dat ...

  2. Q221 最大正方形

    在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积. 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 输出: 4 cla ...

  3. hzjs颠覆jquery,按照中国人思维开发的最简洁的JQUERY替代品更简洁更高效

    颠覆jquery,按照中国人思维开发的最简洁的JQUERY替代品 实现了类似JQUERY的选择器最核心的基本功能 如:$('#image2').attr("src") 另外提供了 ...

  4. php 使用 rabbitmq

    1,配置好rabbitmq 服务器 (参照 http://www.cnblogs.com/spicy/p/7017603.html)(我是linux) 2,新增了一个用户 并点击该用户 增加权限如下

  5. SpringBoot 整合 Logback

    Logback is intended as a successor to the popular log4j project, picking up where log4j leaves off.L ...

  6. 关于chrome浏览器事件拖动的bug(首次点击的时候也触发move的事件)

    在做R80web的时候出现一个奇怪的现象,chorme现在的版本还是存在,拖动事件有mousedown.mousemove.mouseup组成,但是首次click以及失去焦点再重新点击的时候同样会触发 ...

  7. emacs26.1 ppa

    sudo add-apt-repository ppa:kelleyk/emacssudo apt updatesudo apt install emacs26

  8. [作业] Python入门基础---购物车小程序

    1.购物车小程序: 1.1用户输入工资取60% 1.2打印输出商品菜单 1.3由用户输入数字选择 #__author:Mifen #date: 2018/11/27 # 购物车程序 #把工资作为账户的 ...

  9. 【扫盲】】32位和64位Windows的区别

    用户购买windows安装盘或者重新安装操作系统的时候,通常会遇到这个问题,就是不知道该如何选择使用32位操作系统和64位操作系统,有人说64位系统速度快,其实理论上确实是这样,不过具体还要根据你的个 ...

  10. IOS runtime动态运行时二

    在C#.Java中有编译时多态和运行时多态,在OC中,只有运行时的多态,这与它的运行机制有关.OC中,方法的调用是通过消息的传递来进行的.在IOS runtime动态运行时一http://www.cn ...