【问题标题】:AJAX changes content of the string being sent [duplicate]AJAX 更改正在发送的字符串的内容 [重复]
【发布时间】:2017-06-13 09:37:05
【问题描述】:

这快把我逼疯了!

我在 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++ 做了一些非常奇怪的事情。当notenoteHTML 作为字符串设置为C++ 时,到达PHP 端(在note.php 中)的是C 而不是C++!!这对我来说没有任何意义。也许我选择的编码有问题。我尝试了htmlplain/text,但无论出于何种原因,我什至在 php 方面都没有得到任何东西,所以我放弃了进一步研究这个方向。知道为什么吗?

else if ($_POST['what'] == "edit")
{
    printArray($_POST);
}

在浏览器中输出:

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

为什么?

【问题讨论】:

  • + 是一个特殊符号。你需要使用encodeURI
  • 但这不应该由编码本身来处理吗?
  • 您正在发送post,为什么不将数据也作为发布参数发送?
  • stackoverflow.com/a/4276396/4916265abc=encodeURIComponent('a+b+c');
  • 致投反对票的人,不酷也没有理由。这是一个有效的问题,可以在未来帮助很多人。我在询问之前进行了搜索。在那个确切的问题/问题上找不到任何帮助。

标签: javascript php ajax


【解决方案1】:

你必须像这样改变它(在每个元素上使用encodeURIComponent()):

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));

https://stackoverflow.com/a/4276396/4916265

【讨论】:

    猜你喜欢
    • 2021-02-28
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    相关资源
    最近更新 更多