0

我正在尝试通过 javascript 传递使用 php 来编辑数据查询,

我的ajax请求看起来像

var totalSearchResult=10;
$.ajax({
    url:"php/queryManipulation.php",
    type: 'POST',
    data: { totalQuery : totalSearchResult,  query : '{"data":{"match_all":{}}}'},

    success: function(finalList)
    {
        alert(finalList);
    }
});

我的 php 代码看起来像

<?php
$from=$_POST["totalQuery"];
$qry=json_decode($_POST["query"]);
$qry->from=$from;   }?>

我正在尝试以形式获取它,

{"data": {"match_all": {}} , "from": 10}

我得到错误Object of class stdClass could not be converted to string

4

3 回答 3

3

编辑:将 json_decode 返回值从数组更改为对象

您需要在完成编辑后再次对 json 进行编码。所以你可以做的是:

<?php
    $from            = $_POST["totalQuery"];
    $qry             = json_decode($_POST["query"]);
    $qry->data->from = $from;
    //you will get the new json string 
    //as the finalList variable in your post callback
    echo json_encode($qry); 
?>
于 2012-07-24T08:35:50.573 回答
0

You should use json_decode($string, true) - so it will be an array. Details here: http://php.net/manual/en/function.json-decode.php

于 2012-07-24T08:34:11.903 回答
0

您可以解码为数组(不确定对象)并重新编码:

$qry = json_decode($_POST['query'], TRUE);
$qry['from'] = 10;
$new_qry = json_encode($qry);
于 2012-07-24T08:34:43.873 回答