AJAX POST数据被截断

时间:2013-07-24 17:09:26

标签: php ajax post

我正在尝试通过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);

send_email.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,

这显然会截断我试图传递的其余数据。为什么?我究竟做错了什么?谢谢!

3 个答案:

答案 0 :(得分:4)

您需要对数据进行编码,并将其放入参数

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

答案 1 :(得分:1)

您发送的内容类型为“... url-encoded”,同时发送的数据不是网址编码的。您需要在发送数据之前正确编码数据(encodeURIComponent等)

答案 2 :(得分:0)

你只是回应你的问候。

试试这个:

echo  implode($_POST);