0

php 5.4 快速cgi jquery 1.10

jQuery代码:

$.ajax({
          type: "POST",
          url: "",
          dataType: "json",
          data: { json: JSON.stringify({test: 'teste'}) }
        }).done(function(msg) {
              var msg = $.parseJSON(msg);
              alert(msg);
        });

PHP代码:

$json = $_POST['json'];
$info = json_decode($json, true);
var_dump($info);

结果:

array(1) {
  ["test"]=>
  string(5) "teste"
}
null

我不知道为什么这个null apper 以及如何删除它。因为如果我尝试使用:

$i = info['test'];
echo $i;

我会收到: testenull

4

1 回答 1

2

似乎您的JSON数据是问题所在。

json_decode()PHP中,将 JSON 编码的字符串作为输入并将其转换为 PHP 变量。

它像这样工作

<?php
$json = '{"test": 12345}';
$obj = json_decode($json);
print $obj->{'test'}; // 12345
?>
于 2013-09-21T05:41:44.277 回答