Javascript php-json_解码未定义的索引,但值在那里

Javascript php-json_解码未定义的索引,但值在那里,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,我很困惑 我将一个键值对对象从jquery传递到php。并成功地再次提醒它,但如果我转到php页面。它表示数据为空或未定义索引 下面是我的jquery $('#test').click(function(){ var obj= { 'key': 'value', }; $.ajax({ url: "../folder/file.php", type: "POST",

我很困惑

我将一个键值对对象从jquery传递到php。并成功地再次提醒它,但如果我转到php页面。它表示数据为空或未定义索引

下面是我的jquery

$('#test').click(function(){
   var obj= {
                'key': 'value',
            };
$.ajax({
                url: "../folder/file.php",
                type: "POST", 
                data: {
                    'obj' : obj
                },
                dataType: "JSON",
                success:function(data){
                    alert(data);
                }

            });
}); 
下面是我的php

$data = $_POST['obj']; // this is line 1
echo json_encode($data); // this is line 2
使用上述代码,当我单击测试按钮时,我将获得警报
。但是如果我在点击测试按钮后进入php页面。页面显示
注意:未定义索引:第1行为obj,第2行为null。

为什么?


我收到了我输入的值的警报。因此,这一定意味着数据被反复传递。但是php页面说它是空的是一个数组,而不是json字符串

当您在ajax方法中将它用作
data
的值时,它是一个JS对象,除非您明确设置
contentType
,否则它将被转换为post数据。默认情况下,内容类型为
application/x-www-form-urlencoded;字符集=UTF-8

因为您使用的是默认内容类型:

$\u POST['myobj']['key1']
例如应该是key1的值

对对象使用
var\u dump
,以便更好地查看它,并了解它的结构

i、 e


我认为,当您将JSON对象发布到PHP时,您可以通过<代码>php://input包含原始POST数据,因此是需要JSON编码的字符串:

// Read all
$json = file_get_contents('php://input');
// String -> array (note that removing `true` will make it object)
$obj = json_decode($json, true);
// Print it
var_dump($obj);
小型演示(test.php)

最后,如果希望能够同时传递JSON数据和URL参数,请使用以下命令:

function buildRequest(){

    // Get Data
    $json = file_get_contents('php://input');

    // Form the request from the imput
    if ($json!=""){
        $_REQUEST = array_merge_recursive($_REQUEST,json_decode($json, true));
    }
}

buildRequest();
var_dump($_REQUEST);
使用URL和数据参数调用上述函数会导致:

curl -X POST -d '{"test": "value"}' localhost/test.php?param=value2
array(2) {
  ["param"]=>
  string(6) "value2"
  ["test"]=>
  string(5) "value"
}

让我知道上述方法是否适用于您。

报告的行为似乎值得怀疑:
$\u POST['myobj']
将作为一个表达式持续运行;它要么计算为字符串,要么生成“未定义的索引”有效,只需使用与workedmay help hmm var_dump($_POST['myobj'])所使用的路径相同的路径即可。和$_POST['myobj']['key1']都不起作用。如果我更改echo json_encode($data);只是回应。有人向我报警。我认为这与此有关,现在就尝试
var\u dump($\u POST)
。几乎没有逻辑上的理由说明为什么
$data=$\u POST['myobj'];echo json_编码($data)
会像你建议的那样工作,但用我上面提到的代码替换该代码是行不通的。除非你说jQuery返回带有
null
的警报,这是有意义的,因为你显式地将
dataType
设置为
json
。如果你是这么说的,那么从json中删除数据类型,这样你就可以看到数据被转储了。我要么没有收到警报,要么收到对象警报。var_dump($_POST)获取数组(0){}。
$ curl -X POST -d '{"test": "value"}' localhost/test.php
array(0) {
}
array(1) {
  ["test"]=>
  string(5) "value"
}
function buildRequest(){

    // Get Data
    $json = file_get_contents('php://input');

    // Form the request from the imput
    if ($json!=""){
        $_REQUEST = array_merge_recursive($_REQUEST,json_decode($json, true));
    }
}

buildRequest();
var_dump($_REQUEST);
curl -X POST -d '{"test": "value"}' localhost/test.php?param=value2
array(2) {
  ["param"]=>
  string(6) "value2"
  ["test"]=>
  string(5) "value"
}