0
I have the following code, which uses Factory:
angular.module("fluxo", ["ngRoute"]);
.factory('factCliente', ['$http', function($http) {
var _getData2 = function(id_empresa) {
return $http.post("php/index.php", id_empresa);
};
return {
getData2: _getData2
}
}])
.controller("fluxoCtrl", function ($scope, $http, factCliente) {
//var id_empresa = {id: id_empresa};
var id_empresa = {id: 1};
factCliente.getData2(id_empresa).then(function(response) {
$scope.mostraTodasContasEntradas = response;
}, function(error) {
console.log("Ocorreu um erro: " + error);
});
});
And the warning that appears on the console, is this:
"SyntaxError: Unexpected token ["
Php code:
<?php
function mostraContasEntrada($id_empresa){
header('Content-Type: application/json');
$pdo = conectar();
$this->mostraDadosEntrada=$pdo->prepare(
"SELECT c.categoria, sc.subcategoria, data, valor
FROM entrada e
JOIN cat_entradas c
on c.id_categoria = e.categoria
JOIN sub_cat_entrada sc
on sc.id_subcategoria
WHERE id_empresa=:id_empresa
ORDER BY data DESC");
$this->mostraDadosEntrada->bindValue(":id_empresa", $id_empresa);
$this->mostraDadosEntrada->execute();
$return = array();
while ($r = $this->mostraDadosEntrada->fetch(PDO::FETCH_ASSOC)) {
$dataP = explode("-", $r['data']);
$data = $dataP[2].'/'.$dataP[1].'/'.$dataP[0];
$r['data'] = $data;
$r['valor'] = number_format($r['valor'],2,',','.');
$r['subcategoria'] = utf8_encode($r['subcategoria']);
$return[] = $r;
//echo $data.' '.$r['categoria'].' '.utf8_encode($r['subcategoria']).' '.number_format($r['valor'],2,',','.')."<br>";
echo json_encode($return);
}
}
<?
Class and function call:
require_once "../con/conexao.php";
require_once "../classes/contaEntrada.php";
require_once "../classes/contaSaida.php";
$entrada = new contaEntrada();
$saidas = new contaSaida();
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$id_empresa = $request->id_empresa;
$entrada->mostraContasEntrada($id_empresa);
Does anyone know what problem, what syntax error, is this?
Post the complete error, but still check the key locks and parentheses. Initially it has a bracket "[" opening in a wrong place. Make good use of indentation and call angular module again when you can.
– Shura16
As the console says it is syntax error. If you have any idea where the error occurs, you can thresh the code by the browser.
– Pedro Camara Junior
I’ve looked for another clasp that might be open and not closed, but I can’t find it. you can search yourself, because all my code is, here, in the post.
– GustavoSevero
Your JSON return may be invalid, as pointed out by the exception in the console. You can post an example?
– OnoSendai
I put the code here.
– GustavoSevero
A few things I noticed analyzing your code. Your PHP has some syntax errors, such as closing the tag
<?
whereas it should be?>
and some clasps out of place. I recommend to check the closure of everything, because an error in php can cancel all your javascript, even without running the function directly. Another thing, put one onconsole.log(mostraTodasContasEntradas)
At the end of your controller to see what php is returning, sometimes it is a php logic error and not Angularjs. Always search for a debug of the console results to understand the problem. = D– celsomtrindade
@Celsomtrindade, only here I put the closing of the php tag. In my code same, I did not put.
– GustavoSevero
var id_empresa = {id: id_empresa}
This value, it comes from where? And is being generated as?– celsomtrindade
Celso, I put the console.log(displayTodasContasEntrate) and this came up: (id_company) { console.log('company id '+id_enterprise); $http.get("php/index.php? action=showConnect(id_enterprise)"). then( Function(date) { $Scope.accounts = date; //console.lo...
– GustavoSevero
@Celsomtrindade, actually I’m putting this id_company in hand, because I didn’t create Ssion, I’m just testing.
– GustavoSevero
If you use the console, as I said, what result will you get? Not the error that appears directly on the console, but the return you call by inserting the console.log into the controller. So let’s know if json is coming with error or not. I believe the problem is your php return
– celsomtrindade