我正在尝试使用php通过javascript来编辑数据查询,
我的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
发布于 2012-07-24 16:35:51
编辑:已将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-24 16:34:12
您应该使用json_decode($string,true) -因此它将是一个数组。详情请访问:http://php.net/manual/en/function.json-decode.php
发布于 2012-07-24 16:34:44
您可以解码为一个数组(不确定对象),然后重新编码:
$qry = json_decode($_POST['query'], TRUE);
$qry['from'] = 10;
$new_qry = json_encode($qry);https://stackoverflow.com/questions/11627032
复制相似问题