Json解码"试图获得非对象的属性" ERROR

时间:2018-06-08 02:12:18

标签: javascript php angularjs json error-handling

经过几个小时的处理,我将具体。

我试图将数据从角度(视图)上的表传递到控制器,控制器调用工厂,最后向.php文件发出http请求。

它看起来并不那么难。

这是我得到的错误 }

  

尝试在第6行获取非对象的属性

这是我的.php文件(获取简单的选择查询)

    // Including database connections
require_once '../../db/database_connections.php';
// Fetching and decoding the inserted data
$data = json_decode(file_get_contents("php://input"));
$query = "SELECT * from repartidor WHERE id_repartidor = '$data->codigo'"; //THIS is **line 6**
$result = mysqli_query($con, $query);

$arr = array();
$row = mysqli_fetch_assoc($result);
$arr[0] = $row;

// Return json array containing data from the databasecon
echo $json_info = json_encode($arr[0]);
?>

然而,在调试一段时间之后,查询和.php文件的响应确实有效(我证明它用值取代了' $ data-> codigo')。 p>

我认为问题是数据没有到达或被错误解码。

工厂代码:

obtenerItem: function(item){
        return $http.post("http://localhost/sgzena/php/repartidor/repDetail.php", item);
    } //THERE is an update

控制器代码:

$scope.actualizarItem = function (item) {
            repartidorFactory.obtenerItem(item) //aca iba ID
            .then(function (response) {
                var modal = abrirModal(response.data);
                modal.result.then(function(result) {
                    guardarRepartidor(result);
                }); 
            });
        }

查看代码:

    <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
            <thead>
                <tr>
                    <th>Cod.</th>
                    <th>Nombre</th>
                    <th>Telefono</th>
                    <th>
                        <em class="glyphicon glyphicon-cog" tooltip-placement="top" uib-tooltip="Acciones"></em>
                    </th>
                </tr>
            </thead>
            <tbody ng-repeat="item in repartidores">
                <tr class="odd gradeX">
                    <td>{{item.id_repartidor}}</td>
                    <td>{{item.nombre}}</td>
                    <td>{{item.telefono}}</td>
                    <td>
                        <a class="btn btn-default glyphicon glyphicon-edit " tooltip-placement="bottom" uib-tooltip="Editar Repartidor" data-toggle="modal" ng-click="actualizarItem(item)"></a>
                        <a class="btn btn-default glyphicon glyphicon-trash " tooltip-placement="bottom" uib-tooltip="Eliminar Repartidor" ng-click="eliminarItem(item)"></a>
                    </td>
                </tr>
            </tbody>
        </table>

1 个答案:

答案 0 :(得分:0)

这些是我修改的行,代码开始起作用:

旧:

$data = json_decode(file_get_contents("php://input"));
$query = "SELECT * from repartidor WHERE id_repartidor= '$data->codigo'";

解决:

$data = json_decode(file_get_contents("php://input"));
$id_repartidor = mysqli_real_escape_string($con, $data->id_repartidor);
$query = "SELECT * from repartidor WHERE id_repartidor= '$id_repartidor'";