【问题标题】:Issue decoding ajax post in php问题在php中解码ajax帖子
【发布时间】:2017-03-04 06:10:48
【问题描述】:

我正在尝试解码我​​在 javascript 中创建并发送到 php 的对象数组,但是,我似乎无法正确解码它。 带有对象的数组如下所示:

我用来获取信息的 php 代码是这样的:

$allInformation = $_POST['information'];

//this works
$user = $allInformation[count($allInformation)-2];
$num = $allInformation = [count($allInformation)-1];
//this doesnt work 
$array = var_dump(json_decode($allInformation));

数组 1 和 2 的索引工作得很好,但是,我似乎无法解码作为对象的索引 0。我该怎么办?

【问题讨论】:

  • 你是怎么寄的?可能你需要JSON.stringify 信息,然后做json_decode($_POST['information'])
  • 提供更多信息,例如预期结果是什么错误?
  • 我使用 ajax 以以下格式发送它:var data = []; 在我的循环中收集数据,我使用数据数组插入以下内容:data[i] = {'user': username, 'date':date}。然后我将数据按原样传递给 ajax 以发布它。
  • 使用 jQuery 吗?所以你序列化它不是json,只是 var_dump($allInformation)
  • 这是我使用$file = json_decode($_POST['information']); echo $file[0]["user"]; 输出的输出:<br /> <font size='1'><table class='xdebug-error xe-fatal-error' dir='ltr' border='1' cellspacing='0' cellpadding='1'> <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Cannot use object of type stdClass as array in ...

标签: javascript php jquery arrays object


【解决方案1】:

让它工作,JSON 字符串化的 php 数组是 [{"user":"user","date":"03/17/2017"},"user",134997] 我做了 $更新 = json_decode($_POST['信息']);然后循环它以获取用户和日期作为对象,即 $update[$i]->date 。谢谢大家

【讨论】:

    猜你喜欢
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2012-10-17
    相关资源
    最近更新 更多