[英]&-character in Ajax-Requests by POST-Method
I hace the following code-Snippelets 我有以下代码片段
I send this data to a Javascropt-file by a button 我通过按钮将此数据发送到Javascropt文件
<button onclick="sendEntry()" > Senden </button>
sendEntry is defined as follows: sendEntry的定义如下:
.... ....
if(document.getElementById("Eintrag").value==""){
ERROR= ERROR + "Kein Eintrag eingegeben\n";
}
else{
**PARAMS=PARAMS** + "&Eintrag=" + document.getElementById("Eintrag").value;
}
... ...
the result is sent to a Ajax-request: 结果发送到Ajax请求:
ajaxRequest.open("POST", "./../php/MakeEntry.php", true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.send(PARAMS);
.... ....
How can I avoid that the &-char auses a new POST-Parameter as shown in the Graphics ?? 如何避免&-char使用新的POST参数,如图所示?
ajaxRequest.open("POST", "./../php/MakeEntry.php", true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.send(encodeURI(PARAMS));
Try to encode your data ;) 尝试对您的数据进行编码;)
You need to encode the individual values that you are sending in the url so that they will not be interpreted as special characters: 您需要对要在url中发送的各个值进行编码,以使它们不会被解释为特殊字符:
else{
**PARAMS=PARAMS** + "&Eintrag=" + encodeURIComponent(document.getElementById("Eintrag").value);
}
Note that this applies to all values, the example is just for the last one you are adding. 请注意,这适用于所有值,该示例仅适用于您要添加的最后一个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.