How can I send the amp;quot;amp;amp;amp;quot; (ampersand) character via AJAX?(如何通过AJAX发送amp;amp;(和号)字符?)
问题描述
我想用POST
方法从JavaScript发送几个变量和一个字符串。
我从数据库中获取字符串,然后将其发送到一个PHP页面。我正在使用XMLHttpRequest
对象。
问题是字符串中包含字符&
几次,而PHP中的$_POST
数组将其视为多个键。
我尝试使用replace()
函数将&
替换为&
,但似乎没有任何作用。
有人能帮忙吗?
脚本代码和字符串如下:
var wysiwyg = dijit.byId("wysiwyg").get("value");
var wysiwyg_clean = wysiwyg.replace('&','&');
var poststr = "act=save";
poststr+="&titlu="+frm.value.titlu;
poststr+="§iune="+frm.value.sectiune;
poststr+="&wysiwyg="+wysiwyg_clean;
poststr+="&id_text="+frm.value.id_text;
xmlhttp.open("POST","lista_ajax.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(poststr);
字符串为:
<span class="style2">"Busola"</span>
推荐答案
您可以使用encodeURIComponent()。
它将转义URL中不能逐字出现的所有字符:
var wysiwyg_clean = encodeURIComponent(wysiwyg);
在本例中,与号字符&
将替换为转义序列%26
,该序列在URL中有效。
这篇关于如何通过AJAX发送&;&;(和号)字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!