The*_*lob 0 javascript php ajax special-characters
在Firefox和Chrome中,我的php($ _GET)正在接收数字和字母以及特殊字符(例如" - "和"("),但字符除外+.这是我的ajax请求:
function ajaxFunction(param) {
var ajaxRequest;
try {
ajaxRequest = new XMLHttpRequest();
} catch (e1) {
try {
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e2) {
try {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e3) {
alert("Something is wrong here. Please try again!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function () {
if (ajaxRequest.readyState === 4) document.getElementById("myDiv").innerHTML = ajaxRequest.responseText;
};
ajaxRequest.open("GET", "AJAX_file.php?param=" + param, true);
ajaxRequest.send(null);
}
Run Code Online (Sandbox Code Playgroud)
当用户单击按钮调用ajaxFunction()时,用户首先在单击按钮之前填写输入类型="text".如上所述,在Firefox和Chrome中,我的php文件正在接收数字和字母以及特殊字符(例如" - ","("和")")(AJAX_file.php;非常简洁的代码版本如下但你得到了要点)并成功回应:
<?php include 'connect.php';
//Lots of code
echo $_GET['param'];
?>
Run Code Online (Sandbox Code Playgroud)
但是,如果用户输入字符+(n次,其中n> = 1),则没有回显输出.请注意Firebug会看到附加的用户输入(此处显示为"++"):
GET http://www.mywebsite.com/AJAX_file.php?param=++ 200 OK 278ms
Run Code Online (Sandbox Code Playgroud)
我的PHP错误日志显示没有通知,警告,也没有错误.谁能告诉我这里我做错了什么?我正在使用网络托管服务......也许这可能是他们的行动过滤器之一?
+是URL中的保留字符.你必须逃脱它,%2B就像你想发送它一样.
查看您必须逃脱的保留字符列表.你会注意到(,)和-是不是就可以了,但是+是.
不要自己编码/转义.在每种语言和框架中都有一种处理方法.在JavaScript中,使用encodeURIComponent():
ajaxRequest.open("GET", "AJAX_file.php?param=" + encodeURIComponent(param), true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |