Javascript 如何通过 AJAX 发送“&”(与号)字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11294107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:20:09  来源:igfitidea点击:

How can I send the "&" (ampersand) character via AJAX?

javascriptajaxstringpostxmlhttprequest

提问by candino

I want to send a few variables and a string with the POSTmethod from JavaScript.

我想用POSTJavaScript 中的方法发送一些变量和一个字符串。

I get the string from the database, and then send it to a PHP page. I am using an XMLHttpRequestobject.

我从数据库中获取字符串,然后将其发送到 PHP 页面。我正在使用一个XMLHttpRequest对象。

The problem is that the string contains the character &a few times, and the $_POSTarray in PHP sees it like multiple keys.

问题是字符串&多次包含该字符,而$_POSTPHP 中的数组将其视为多个键。

I tried replacing the &with \&with the replace()function, but it doesn't seem to do anything.

我试着更换&\&replace()功能,但它似乎并没有做任何事情。

Can anyone help?

任何人都可以帮忙吗?

The javascript code and the string looks like this:

javascript 代码和字符串如下所示:

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

The String is:

字符串是:

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

回答by Frédéric Hamidi

You can use encodeURIComponent().

您可以使用encodeURIComponent()

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

它将转义所有不能在 URL 中逐字出现的字符:

var wysiwyg_clean = encodeURIComponent(wysiwyg);

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

在此示例中,与号字符&将替换为转义序列%26,这在 URL 中有效。

回答by Wolfram

You might want to use encodeURIComponent().

您可能想要使用encodeURIComponent()

encodeURIComponent("&quot;Busola&quot;"); // => %26quot%3BBusola%26quot%3B

回答by Ned Batchelder

You need to url-escape the ampersand. Use:

您需要对&符号进行网址转义。用:

var wysiwyg_clean = wysiwyg.replace('&', '%26');

As Wolfram points out, this is nicely handled (along with all the other special characters) by encodeURIComponent.

正如 Wolfram 指出的那样,encodeURIComponent 很好地处理了这一点(以及所有其他特殊字符)。

回答by Nadav S.

Ramil Amr's answer works only for the & character. If you have some other special characters, you should use PHP's htmlspecialchars()andJS's encodeURIComponent().

Ramil Amr 的回答仅适用于 & 字符。如果你有其他一些特殊字符,你应该使用 PHPhtmlspecialchars()JS 的encodeURIComponent().

You can write:

你可以写:

var wysiwyg_clean = encodeURIComponent(wysiwyg);

And on the server side:

在服务器端:

htmlspecialchars($_POST['wysiwyg']);

This will make sure that AJAX will pass the data as expected, and that PHP (in case your'e insreting the data to a database) will make sure the data works as expected.

这将确保 AJAX 将按预期传递数据,并且 PHP(以防您将数据插入数据库)将确保数据按预期工作。

回答by Christopher Tokar

The preferred way is to use a JavaScript library such as jQuery and set your data option as an object, then let jQuery do the encoding, like this:

首选方法是使用 JavaScript 库(例如 jQuery)并将数据选项设置为对象,然后让 jQuery 进行编码,如下所示:

$.ajax({
  type: "POST",
  url: "/link.json",
  data: { value: poststr },
  error: function(){ alert('some error occured'); }
});

If you can't use jQuery (which is pretty much the standard these days), use encodeURIComponent.

如果您不能使用 jQuery(这几乎是当今的标准),请使用encodeURIComponent

回答by user10164982

encodeURIComponent(Your text here);

This will truncate special characters.

这将截断特殊字符。

回答by nikhilmeth

You can pass your arguments using this encodeURIComponent function so you don't have to worry about passing any special characters.

您可以使用此 encodeURIComponent 函数传递参数,因此您不必担心传递任何特殊字符。

data: "param1=getAccNos&param2="+encodeURIComponent('Dolce & Gabbana') 

OR

或者

var someValue = 'Dolce & Gabbana';
data: "param1=getAccNos&param2="+encodeURIComponent(someValue)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

回答by Sirko

You could encode your string using Base64 encoding on the JavaScript side and then decoding it on the server side with PHP (?).

您可以在 JavaScript 端使用 Base64 编码对字符串进行编码,然后在服务器端使用 PHP (?) 对其进行解码。

JavaScript (Docu)

JavaScript (文档)

var wysiwyg_clean = window.btoa( wysiwyg );

PHP (Docu):

PHP(文档):

var wysiwyg = base64_decode( $_POST['wysiwyg'] );