【问题标题】:how to get the value of multidimensional array pass from jquery and ajax?如何从 jquery 和 ajax 获取多维数组传递的值?
【发布时间】:2014-11-06 03:53:21
【问题描述】:

我将一个多维数组从 ajax.js 发送到 test1.php。但后来我不知道如何在我的 test1.php 中检索多维数组。我找到了很多例子,但它们对我不起作用。

ajax.js

answers = {};
answers[0] = {};
answers[0]['question_id'] = '12';
answers[0]['answer_id'] = '32';

$.ajax({
    type: "POST",
    url: 'test1.php',
    data: {answers: answers},
    //dataType: "json",

    error: function(jqXHR, textStatus, errorThrown){
        alert(errorThrown);
    },
    success: function(data){
        alert('success!');
    }
});

test1.php

$a=$_POST['answers'][0]['answer_id'];
echo "$a"; //display nothing, I wish to display '32' here

感谢任何帮助。谢谢!

更新:我包含库并且 Ajax.js 确实给了我一个警报“成功”。所以这不是图书馆的问题。再次感谢您的帮助!

【问题讨论】:

  • 也许 jQuery 以 JSON 格式发送它。尝试使用json_decode。尝试转储$_POST 以确保您得到什么。
  • @SebastianOsuna json_decode()?不。您已经可以直接从$_POST 获得,无需解码任何内容,OP 需要检查的是 PHP 文件的路径是否正确,并检查控制台浏览器是否有错误。并确保 jquery 确实在那里
  • 它正在工作.. 你包含图书馆了吗?
  • 哪个库?抱歉,我真的是 ajax 新手。感谢您的帮助!
  • 您创建的是对象,而不是数组,只需执行print_r($_POST['answers']); 并查看它返回的结构

标签: php jquery arrays ajax


【解决方案1】:

试试这个 -

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
answers = {};
answers[0] = {};
answers[0]['question_id'] = '12';
answers[0]['answer_id'] = '32';

$.ajax({
    type: "POST",
    url: 'test1.php',
    data: {answers: answers},
    //dataType: "json",

    error: function(jqXHR, textStatus, errorThrown){
        alert(errorThrown);
    },
    success: function(data){
        alert('success!');
    }
});
</script>

test1.php

<?php
$a=$_POST['answers'][0]['answer_id'];
echo "$a hello"; //display nothing, I wish to display '32' here

【讨论】:

  • 是的,我已经包含了库,并且 Ajax.js 确实给了我一个“成功”警报。所以这不是图书馆的问题。再次感谢您的帮助!
  • 请求成功了吗?
  • $a 为空。 test1.php 中没有显示任何内容。我认为它可能不会成功。
  • 使用开发者工具并在 netwrok 选项卡下查看。你会在那里响应和请求。
猜你喜欢
  • 1970-01-01
  • 2014-09-15
  • 2011-07-09
  • 2015-01-16
  • 2020-07-21
  • 1970-01-01
  • 2018-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多