我怎么能发送"&安培;" (符号)通过AJAX的人物? [英] How can I send the "&" (ampersand) character via AJAX?

查看:120
本文介绍了我怎么能发送"&安培;" (符号)通过AJAX的人物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想送一些变量和一个字符串与JavaScript的POST方法。我从数据库中获取的字符串,然后将其发送到PHP页面。我使用XMLHtt prequest对象。 的问题是该字符串中包含的字符与&几声,$ _ POST数组在PHP中看到它像多个密钥。 我试图取代&放大器;用\&安培;替换()函数,但似乎并没有做任何事情。 任何人都可以帮忙吗?

JavaScript的code和字符串看起来是这样的:

  VAR所见即所得= dijit.byId(所见即所得)获得(价值)。
变种wysiwyg_clean = wysiwyg.replace('&安培;','\&安培;');

VAR poststr =ACT =保存;
poststr + =&放大器; titlu =+ frm.value.titlu;
poststr + =&放大器; sectiune =+ frm.value.sectiune;
poststr + =&放大器;所见即所得=+ wysiwyg_clean;
poststr + =&放大器; id_text =+ frm.value.id_text;
xmlhttp.open(POST,lista_ajax.php,真正的);
xmlhttp.setRequestHeader(内容型,应用程序/ x-WWW的形式urlen codeD);
xmlhttp.send(poststr);
 

字符串为:

 <跨度类=蓝紫魅力>&安培; QUOT; Busola和放大器; QUOT;< / SPAN>
 

解决方案

您可以使用<一个href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/en$c$cURIComponent">en$c$cURIComponent().

这会逃避所有的网址不能逐字出现字符:

  VAR wysiwyg_clean = EN codeURIComponent(所见即所得);
 

在这个例子中,符号字符&安培; 将被转义序列替换%26 ,这是有效的网址。

I want to send a few variables and a string with POST method from JavaScript. I get the string from the database, and then send it to a PHP page. I am using XMLHttpRequest object. The problem is that the string contains the character "&" few times, and $_POST array in PHP sees it like multiple keys. I tried replacing the "&" with "\&" with replace() function, but doesn't seem to do anything. Can anyone help?

The javascript code and the string looks like this:

    var wysiwyg = dijit.byId("wysiwyg").get("value");
var wysiwyg_clean = wysiwyg.replace('&','\&');

var poststr = "act=save";
poststr+="&titlu="+frm.value.titlu;
poststr+="&sectiune="+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);

String is:

 <span class="style2">&quot;Busola&quot;</span>

解决方案

You can use encodeURIComponent().

It will escape all the characters that cannot occur verbatim in URLs:

var wysiwyg_clean = encodeURIComponent(wysiwyg);

In this example, the ampersand character & will be replaced by the escape sequence %26, which is valid in URLs.

这篇关于我怎么能发送&QUOT;&安培;&QUOT; (符号)通过AJAX的人物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆