ajax - 验证时防止ajax中的&符号破坏?
问题描述
function authenticate(){
var email = 'john.doe@gmail.com';
var password = '123&567';
var ajaxRequest;
credentials = "cmd=login&email="+email+"&password="+password;
var ajaxRequest = new XMLHttpRequest();
ajaxRequest.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
ajaxRequest.open('POST', 'http://xx', true);
ajaxRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
ajaxRequest.send(credentials);
return false;
}
调用函数后结果如下:
cmd=login&email="+"john.doe@gmail.com"+"&password="+"123&567";
解决方案
您可以使用该功能encodeURIComponent
对密码进行编码:
var email = 'john.doe@gmail.com';
var password = '123&567';
var ajaxRequest;
credentials = "cmd=login&email="+email+"&password="+encodeURIComponent(password);
console.log(credentials);
/* ... */
在你的 PHP 中,你可以使用urldecode
它来解码它。
推荐阅读
- php - 没有标头凭据的 XML Soap 请求
- outlook-restapi - 具有多/单值扩展属性的 Outlook REST api 丰富通知不起作用
- function - 对于那些使用 WooCommerce 的人
- angularjs - 在赛普拉斯中找不到可拖动属性
- c++ - 用向量的向量的元素初始化向量的空向量
- azure - 使用 Azure Devops Pipeline 的 Docker .NET(非核心)
- google-apps-script - 在 G-Suite 插件中获取 CalendarEvent 的重现
- javascript - 将文件发送给没有响应功能的客户端
- java - 线程“main”中的异常 java.lang.NoSuchMethodError: 'void scala.util.matching.Regex.
(java.lang.String, scala.collection.Seq)' - git - Git 从不同的仓库推送和拉取