【问题标题】:Stringified data sent via Jquery post is unusable通过 Jquery post 发送的字符串化数据不可用
【发布时间】:2014-09-16 19:25:20
【问题描述】:

我在 javascript 中对一个对象进行字符串化,并通过 Jquery post 方法发送它:

reservation={};
count=0;
$(".reservation").each(function() {
    reservation[this.id]=jQuery(this).val();
    count++;
    if (count==jQuery(".reservation").length)
    {
        reservationString=JSON.stringify(reservation);
        $.post("helper.php", {reservation: reservationString}) .done(function(data) {   

“helper.php”文件获取数据,但是引号都被转义了,所以当我尝试解码时,我得到一个json错误,错误代码为4。

这里是 helper.php 文件:

$reservation=$_REQUEST['reservation'];
echo $reservation . "\n";
$reservation=json_decode(html_entity_decode($reservation));

我尝试在javascript中提醒reservationString,字符串没有在那里转义,所以很可能jQuery post方法为我转义了字符串。可能是什么问题,你们能提出解决方案吗?

附加信息: 这是我在 javascript 中警告出字符串化对象后得到的结果:

{"test":"bubub","Domain":"online","Type":"banner","s2id_CompanyId":"","CompanyId":"-","ContactPersonId":"0"}

这是我在 php 中回显数据后得到的:

{\"test\":\"bubub\",\"Domain\":\"online\",\"Type\":\"banner\",\"s2id_CompanyId\":\"\",\"CompanyId\":\"-\",\"ContactPersonId\":\"0\"}

【问题讨论】:

  • 检查你的服务器上是否启用了魔术引号,是否禁用它,或者如果你不能,请在$reservation上使用stripslashes
  • stripslashes 现在完成了这项工作,但我也在检查魔术引号。谢谢!

标签: javascript php jquery json stringify


【解决方案1】:

这应该有效:

$reservation=$_REQUEST['reservation'];
echo $reservation . "\n";
$reservation=stripslashes(json_decode(html_entity_decode($reservation));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2011-06-30
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多