【发布时间】:2014-11-12 17:16:18
【问题描述】:
我用谷歌搜索了这个但没有运气。
我有一个文本区域,我正在尝试使用 jQuery 通过 ajax 发送。我的问题是当 textarea 包含&符号(&)时它会中断。也就是说,& 之后包含的任何文本都不会被提取,包括与号 (&)。我发送数据如下:
message = "What are the concerns you are looking to address? "+othr+".\n";
var dataString = 'name=' + name + '&email=' + email + '&company=' + company + '&message=' + message + '&othr=' + othr + '&vpb_captcha_code=' + vpb_captcha_code + '&submitted=1';
$.ajax({
type: "POST",
url: "contactus-contact-form.php",
data: dataString ,
我阅读了encodeURIComponent() 并尝试实现相同但它不起作用。有什么指点吗?
编辑:以下获取文本值失败:
message = "What are the concerns or security issues you are looking to address? "+othr+".\n";
var data0 = {name: + name + email: + email + company: + company + message: + message +
othr: + othr + vpb_captcha_code: + vpb_captcha_code + submitted: "1"};
$.ajax({
type: "POST",
url: "contactus-contact-form.php",
data: data0,
contentType: "application/json; charset=utf-8",
dataType: "json",
【问题讨论】:
-
您可以在处理之前简单地将所有
&替换为&。否则使用serialize(),或具有命名属性的数据对象,而不是连接字符串(无论如何这很容易出错)。 -
为什么将数据作为字符串发送?你可以把它作为一个对象发送,jQuery 会处理一切。
-
@TrueBlueAussie 我该如何替换它? textarea 是用户用来发送查询的表单的一部分。
-
@MMM 请看上面的编辑
-
@Sarah:那是无效的Javascript,你想做
{name: name, email: email}..
标签: javascript jquery html ajax