How to get object in php

0

Good evening guys,

I'm trying to pass parameters from my controller to php that does search in the database and I'm not getting it. I'm passing the data as json object.

controller:

var buscaCategorias = function(){

    var idempresa = $window.localStorage.getItem('idemp');
    var opcao = 'pegarCategoria';
    var buscaCat = {
        "opcao": opcao, 
        "idempresa": idempresa
    };

    console.log(buscaCat);

    $http.post('http://localhost:8888/sistemas/webApps/fluxo_de_caixa/fluxojoin_2.0/php/buscaCatSubcat.php', buscaCat).success(function(data){
        console.log(data);
    });
};

buscaCategorias();
  

In this console.log (searchCat), what shows is Object {option: "getCategory", idempresa: "3"}

PHP:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

include_once("con.php");

$pdo = conectar();

$data = file_get_contents("php://input");
$data = json_decode($data);

$opcao = $data->opcao;

switch ($opcao) {
    case 'pegarCategoria':

        $idempresa = $data->idempresa;

        $buscaCategoria=$pdo->prepare("SELECT categoria, idcategoria FROM categoria WHERE empresa_idempresa=:idempresa ");
        $buscaCategoria->bindValue("idempresa", $idempresa);
        $buscaCategoria->execute();

        $return = array();

        while ($linhaCat=$buscaCategoria->fetch(PDO::FETCH_ASSOC)) {

            $linhaCat['categoria'] = $linhaCat['categoria'];
            $linhaCat['idcategoria'] = $linhaCat['idcategoria'];
            $return = $linhaCat;

        }

        echo json_encode($return);

        break;
    
asked by anonymous 13.12.2016 / 23:56

1 answer

3

There are some problems there.

First, the GET verb has no body, the data must be passed in the URL via querystring .

So, if you open the $http.get documentation you will notice that the second parameter is a configuration object and not the data you want to pass to the server side . The documentation says:

  

config | Object | Optional configuration object

Just knowing this you will need to change some things (in the application and also rethink what you are doing).

But it also has another problem on the PHP side.

file_get_contents("php://input"); serves to get data received by POST and not GET .

Possibly only changing the GET request to POST will already show results.

$http.post('http://urlEnormeAqui/buscaCatSubcat.php', buscaCat).success(function(){ });
    
14.12.2016 / 00:11