問題描述
我想用 JavaScript 的 POST
方法發(fā)送一些變量和一個字符串.
I want to send a few variables and a string with the POST
method from JavaScript.
我從數(shù)據(jù)庫中獲取字符串,然后將其發(fā)送到 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
數(shù)組把它看成是多個鍵.
The problem is that the string contains the character &
a few times, and the $_POST
array in PHP sees it like multiple keys.
我嘗試將 &
替換為 &
與 replace()
函數(shù),但它似乎沒有做任何事情.
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().
它將轉(zhuǎn)義所有不能在 URL 中逐字出現(xiàn)的字符:
It will escape all the characters that cannot occur verbatim in URLs:
var wysiwyg_clean = encodeURIComponent(wysiwyg);
在此示例中,與字符 &
將被替換為轉(zhuǎn)義序列 %26
,這在 URL 中有效.
In this example, the ampersand character &
will be replaced by the escape sequence %26
, which is valid in URLs.
這篇關(guān)于如何發(fā)送“&"?通過 AJAX 的(和號)字符?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!