说明: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. localhost, 127.0.0.1, 0.0.0.0

    总结: localhost:是一个域名.域名可以认为是某个ip的别称,便于记忆.通常localhost对应的ip是127.0.0.1,不过这个也可以设置,参见知乎回答 127.0.0.1:是一个回环地 ...

  2. [原创]php任务调度器 hellogerard/jobby

    目录 简介 安装 标准使用 选项 项目实践 简介 一个强大的php层面上的定时任务调度器, 无需修改系统层面的crontab 实际中, php 结合 crontab 来实现定时任务是一种经得住考验的实 ...

  3. 前端知识总结--BFC

    Block Formatting Context,中文直译为块级格式上下文. 1. BFC的定义 是 W3C CSS 2.1 规范中的一个概念,它决定了元素如何对其内容进行定位,以及与其他元素的关系和 ...

  4. 06-图2 Saving James Bond - Easy Version (25 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  5. js对象注意

    var obj = {}; //定义一个对象 obj[1] = 1; //设置obj对象的属性为数字1属性,属性值为1 obj['1'] = 2398; //设置obj对象的属性为字符串1属性,属性值 ...

  6. JSONP-跨域读取数据

    页面代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...

  7. 2018青岛网络赛G - Couleur 区间上的启发式合并

    题意:给出\(a[1...n]\),共\(n\)次操作,每次删除一个位置\(p_i\)(强制在线),此时区间会变为两个分离的区间,求每次操作的最大区间逆序对 首先要知道必要的工具,按权值建立的主席树可 ...

  8. JS实现值复制

    在JS中对象一般都是传地址,后续修改也会影响原始数据.例如这样. var a={ b:"b" }; var c=a; c.b="c"; console.log( ...

  9. React 的几个需要注意的地方

    1.写组件时,最好将一个大的组件分解成多个小的组件. 通过React写组件时,应当尽可能地将组件分为更小的更多的组件,然后再复合组件. 比如下面的评论组件就是一个组件,一个庞大的组件,这时我们还没有将 ...

  10. java 并发编程——Thread 源码重新学习

    Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...