使用AngularJS和PHP进行CRUD

时间:2016-01-21 16:15:43

标签: javascript php angularjs crud

我有以下代码:

var carregarUsuario = function () {
    $http.get("buscar.php").then(function (retorno){
        console.log(retorno.data);
        $scope.usuarios = retorno.data;
    });
};
carregarUsuario();

PHP:

error_reporting(0);
$user = "root";
$password = "";
$db = "angulardb";
$host = "localhost";
$con = mysqli_connect("localhost", $user, $password, $db);
if (mysqli_connect_errno()){
  echo "Erro: " . mysqli_connect_error();
  }
$usuario = mysqli_query($con, "SELECT *  FROM users");

    header('Content-Type: application/json');
    $return = array();

    while ($dados = mysqli_fetch_assoc($usuario)) {
        array_push($return, $dados);
    }

    echo json_encode($return);

控制台或页面上未显示任何数据。我该如何解决这个问题?

更新

当我在我的PHP代码中放置print_r时,就在while之后(在.php页面中):

Array
(
    [0] => Array
        (
            [0] => 2
            [id] => 2
            [1] => João Silva
            [nome] => João Silva
            [2] => joao.silva@angular.com
            [email] => joao.silva@angular.com
            [3] => 123456
            [pass] => 123456
        )

    [1] => Array
        (
            [0] => 3
            [id] => 3
            [1] => Mario de Almeida
            [nome] => Mario de Almeida
            [2] => mario.almeida@angular.com
            [email] => mario.almeida@angular.com
            [3] => 123456
            [pass] => 123456
        )

)

仅使用console.log(retorno)我有:

Object {data: "", status: 200, config: Object, statusText: "OK"} 

控制台或页面上未显示任何数据。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

老兄,你不能回声阵。使用print_r(json_decode($array))

var carregarUsuario = function () {
    $http.get("buscar.php").then(function (retorno){
        console.log(retorno.data);
        $scope.usuarios = retorno.data;
    });
 return $scope.usuarios
};
carregarUsuario();

此外,你的功能不会返回任何内容。

答案 1 :(得分:0)

问题解决了。显然,错误发生在数据库的整理中。银行就像拉丁语,转移到utf8并正常运作。谢谢