How can I send the quot;amp;quot; (ampersand) character via AJAX?(如何发送“amp;?通过 AJAX 的(和号)字符?)
问题描述
我想用 JavaScript 的 POST
方法发送一些变量和一个字符串.
I want to send a few variables and a string with the POST
method from JavaScript.
我从数据库中获取字符串,然后将其发送到 PHP 页面.我正在使用 XMLHttpRequest
对象.
I get the string from the database, and then send it to a PHP page. I am using an XMLHttpRequest
object.
问题是字符串多次包含字符&
,而PHP中的$_POST
数组把它看成是多个键.
The problem is that the string contains the character &
a few times, and the $_POST
array in PHP sees it like multiple keys.
我尝试将 &
替换为 &
与 replace()
函数,但它似乎没有做任何事情.
I tried replacing the &
with &
with the replace()
function, but it doesn't seem to do anything.
谁能帮忙?
javascript 代码和字符串如下所示:
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 中逐字出现的字符:
It will escape all the characters that cannot occur verbatim in URLs:
var wysiwyg_clean = encodeURIComponent(wysiwyg);
在此示例中,与字符 &
将被替换为转义序列 %26
,这在 URL 中有效.
In this example, the ampersand character &
will be replaced by the escape sequence %26
, which is valid in URLs.
这篇关于如何发送“&"?通过 AJAX 的(和号)字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何发送“&"?通过 AJAX 的(和号)字符?


基础教程推荐
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01