$ http.get无法从PHP后端检索数据

时间:2019-11-02 11:42:48

标签: php html angularjs

AngularJS,PHP和MySQL:在ng-change上更改ng-repeat数据

我曾经在基于Web的系统上工作,ng-repeat似乎在重复键上有错误,我不知道该怎么办。 selectall品牌似乎工作正常,但我在下面粘贴的php代码似乎不起作用,并且“按$ index跟踪”。下面的代码仅显示无限循环或表中的行

html代码

<div class="card-body p-0">
      <!-- DataTales Example -->
      <div class="card shadow mb-4" style="font-size: 0.75rem">
        <div class="card-header py-2">
          <h6 class="m-0 font-weight-bold text-green">Category of {{selectedBrand}}</h6>
        </div>
        <div class="card-body">
          <div class="table-responsive col-xl-12 col-lg-12 col-xl-12"
               ng-disabled="disableTable">
            <table class="table-letters-lg table-hover" id="dataTable" width="0%" cellspacing="0">
              <thead>
                <tr class="bg-gradient-primary" style="color: white">
                  <th>Category ID</th>
                  <th>Category Name</th>
                  <th>Category Code</th>
                </tr>
              </thead>
              <tbody>
                <tr style="cursor: pointer" ng-repeat="categories in prodcategory">
                  <td>{{categories.category_id}}</td>
                  <td>{{categories.category_name}}</td>
                  <td>{{categories.category_code}}</td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>

    </div>
</div>

angularjs代码

$http.get('database/prodcategory/selectallbrandname_category.php')
  .then(function(z){
    $scope.prodbrand_category = z.data;
});

$http.get('database/prodcategory/selectallbrand_category.php')
  .then(function(y){
    $scope.prodcategory = y.data;
});

$scope.selectBrand = function() {
    if($scope.selectedBrand == "All Brands") {
        $scope.showAllCategoryList();
    }

    else {
        var b_name = $scope.selectedBrand;
        $scope.showBrandCategoryList(b_name);
    }
}
$scope.showAllCategoryList = function() {
    $http.get('database/prodcategory/selectallbrand_category.php')
      .then(function(response){
        $scope.prodcategory = response.data;
    });
}

$scope.showBrandCategoryList = function(brand_name) {
    $http.get('database/prodcategory/selectbrand_category.php', {brand_name: brand_name})
      .then(function(x){
        $scope.prodcategory = x.data;
    });
}

php代码

<?php
    require_once '..\connect.php';
    $data = json_decode(file_get_contents("php://input"));
    $brand_name = $data->brand_name;

    try {
        $stmt = $pdo->prepare("SELECT * FROM `prodcategory` INNER JOIN `prodbrand` ON `category_brandid` = `brand_id` WHERE `brand_name` = ?");

        $stmt -> execute([$brand_name]);

        $data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
        echo json_encode($data);
    }catch (Exception $e){
        echo $e;
        $pdo->rollback();
        throw $e;
    }
?>

2 个答案:

答案 0 :(得分:0)

数小时来查看angularjs代码。似乎$ http.get方法无效,因此我将其更改为$ http.post。

$scope.showAllCategoryList = function() {
    $http.post('database/prodcategory/selectallbrand_category.php')
      .then(function(response){
        $scope.prodcategory = response.data;
    });
}

$scope.showBrandCategoryList = function(brand_name) {
    $http.post('database/prodcategory/selectbrand_category.php', {brand_name: brand_name})
      .then(function(x){
        $scope.prodcategory = x.data;
    });
}

答案 1 :(得分:0)

$http.get操作之一格式错误。

  

错误

$scope.showBrandCategoryList = function(brand_name) {
    $http.get('database/prodcategory/selectbrand_category.php', {brand_name: brand_name})
      .then(function(x){
        $scope.prodcategory = x.data;
    });
}

$http.get方法的第二个参数不携带数据,它携带可选的config对象。

从文档中:

  

$http.get(url, [config]);

     
      
  • url string正在请求的资源的绝对或相对URL
  •   
  • config(可选)Object可选配置对象。参见$http() arguments
  •   
     

AngularJS $http Service API Reference - $http.get