簡體   English   中英

$http.get 無法從 PHP 后端檢索數據

[英]$http.get failing to retrieve data from PHP backend

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;
    }
?>

花了幾個小時查看 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;
    });
}

$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 object。

從文檔:

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

  • url string被請求資源的絕對或相對 URL
  • 配置(可選) Object可選配置 object。 $http() arguments

AngularJS $http 服務 API 參考 - $http.get

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM