Javascript AJAX更改正在发送的字符串的内容

Javascript AJAX更改正在发送的字符串的内容,javascript,php,ajax,Javascript,Php,Ajax,这快把我逼疯了 我在JS中有类似的内容: xmlhttp.open("POST", "/note.php", true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); console.log(" >>>> " + note + " " + noteHTML); xmlhttp.send("what=edit&note="

这快把我逼疯了

我在JS中有类似的内容:

xmlhttp.open("POST", "/note.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
console.log(" >>>> " + note  + " " + noteHTML);
xmlhttp.send("what=edit&note=" + note + "&noteHTML=" + noteHTML + "&noteIdx=" + noteIdx);
控制台中的输出:

 >>>> C++ C++
这段代码正确地发送了大部分文本,也就是
note.php
中的内容,我得到的内容与JS端的内容相同,但不管出于什么原因,它对字符串
C++
做了一些非常奇怪的事情。当字符串
note
noteHTML
被设置为
C++
时,到达PHP端(在
note.PHP
中)的是
C
而不是
C++
!!这对我来说毫无意义。也许我选择的编码有问题。我尝试了
html
plain/text
,但无论出于什么原因,我在php方面都没有得到任何东西,所以我放弃了进一步研究这个方向。知道为什么吗

else if ($_POST['what'] == "edit")
{
    printArray($_POST);
}
在浏览器中输出:

what => edit note => C noteHTML => C noteIdx => 17no

为什么?

您必须这样更改它(在每个元素上使用
encodeURIComponent()
):


+
是一种特殊符号。您需要使用
encodeURI
,但这不应该由编码本身处理吗?您正在发送
post
为什么不将数据作为post参数发送呢<代码>abc=encodeURIComponent('a+b+c')xmlhttp.open("POST", "/note.php", true); xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); console.log(" >>>> " + note + " " + noteHTML); xmlhttp.send("what=edit&note=" + encodeURIComponent(note) + "&noteHTML=" + encodeURIComponent(noteHTML) + "&noteIdx=" + encodeURIComponent(noteIdx));