0

我正在尝试通过 AJAX POST 将较大的数据集传递给 PHP。我的数据被截断,但我不明白为什么。

var greeting     = tinyMCE.get("greeting").getContent();
...
var content = "subject="          +subject+
              "&greeting="        +greeting+
              "&results="         +results+
              "&upcoming="        +upcoming+
              "&thisweek="        +thisweek+
              "&signoff="         +signoff;

console.log(content);   //<--see below for this output
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "scripts/send_email.php", true);
xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xmlhttp.send(content);

发送电子邮件.php

$greeting       = $_POST['greeting'];
echo $greeting;
die();

这是我的内容变量的控制台输出(请注意,在您看到格式的地方,控制台输出 HTML 标记,但我不知道如何在此处显示标记。)

> subject=test&greeting=<p class="p1"><strong>Hello all,</strong></p> <p
> class="p2">&nbsp;</p> <p class="p1">&nbsp;</p> <p class="p3">This is a
> test. I am just typing some random stuff to verify that all of my data
> is getting passed correctly over to PHP. However, it seems that this
> data is being truncated for reasons that I cannot explain. Why would
> this happen. How can I get all of this data to pass correctly? It
> doesn&rsquo;t make any sense to me as I am using an AJAX POST call and
> not a GET call, so my data length should not be arbitrarily
> limited.</p>&results=<p><strong><span style="text-decoration:
> underline;">RESULTS</span></strong><br /><br /><br
> /></p>&upcoming=<p><strong><span style="text-decoration:
> underline;">UPCOMING EVENTS</span></strong><br /><br /><br
> /></p>&thisweek=<p><strong><span style="text-decoration:
> underline;">THIS WEEK</span></strong><br /><br /><br
> /></p>&signoff=<p>See you out there.</p>"

但是,我的 php echo 语句只输出这个:

Hello all,

这清楚地截断了我试图忽略的其余数据。为什么?我究竟做错了什么?谢谢!

4

3 回答 3

5

您需要对输入参数的数据进行编码

var content = "subject=" + encodeURIComponent(subject) +

等等

于 2013-07-24T17:13:32.227 回答
1

您发送的内容类型为“... url-encoded”,而发送的数据未经过 url 编码。您需要在发送数据之前正确编码数据(encodeURIComponent 等)

于 2013-07-24T17:14:34.540 回答
0

你只是回应你的问候。

试试这个:

echo  implode($_POST);
于 2013-07-24T17:14:13.220 回答