Javascript AJAX更改正在发送的字符串的内容
这快把我逼疯了 我在JS中有类似的内容: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¬e="
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¬e=" + note + "¬eHTML=" + noteHTML + "¬eIdx=" + 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¬e=" + encodeURIComponent(note) + "¬eHTML=" + encodeURIComponent(noteHTML) + "¬eIdx=" + encodeURIComponent(noteIdx));