【问题标题】:Angular JS unable to get param valuesAngular JS 无法获取参数值
【发布时间】:2017-03-01 12:49:26
【问题描述】:

您好,我正在尝试使用 php 和 AngularJS 获取特定用户的数据,但 AngularJS 没有在我的代码中添加参数我尝试了所有方法但没有任何效果,我不知道我使用 HTTP 有什么问题。获取过程

这是我的 JS 代码

var app = angular.module('myApp', ['ui.bootstrap']);

app.filter('startFrom', function() {
    return function(input, start) {
        if(input) {
            start = +start; //parse to int
            return input.slice(start);
        }
        return [];
    }
});
app.controller('customersCrtl', function ($scope, $http, $timeout) {
    $http.get('pages/modVehiclePayments/getPendingPO.php', {
        params: {
            source: link,
            getvendornumber: user.phonenum
        }
     }).success(function(data){
        $scope.list = data;
        $scope.currentPage = 1; //current page
        $scope.entryLimit = 50; //max no of items to display in a page
        $scope.filteredItems = $scope.list.length; //Initially for no filter  
        $scope.totalItems = $scope.list.length;
    });
    $scope.setPage = function(pageNo) {
        $scope.currentPage = pageNo;
    };
    $scope.filter = function() {
        $timeout(function() { 
            $scope.filteredItems = $scope.filtered.length;
        }, 10);
    };
    $scope.sort_by = function(predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };
});

这是我的 PHP

$VendorNum = $_GET['getvendornumber'];

$query="SELECT * FROM tblvendors WHERE VendorNo='$VendorNum' AND statuscode='2'";

$result = $conn->query($query) or die($conn->error.__LINE__);
$arr = array();
if($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $arr[] = $row;  
    }
}
# JSON-encode the response
$json_response = json_encode($arr);

// # Return the response
echo $json_response;

【问题讨论】:

  • 您是否已注销数据 ($scope.list)?是你所期望的,除了减去你期望的参数吗?
  • 确保您的角度版本低于 1.6。 success 不适用于 1.6 版以上
  • 将 'console.dir(data) 或 console.log(data)' 放入成功函数并共享输出。

标签: javascript php angularjs


【解决方案1】:

首先,您从select 查询中调用数组数据,如下所示。

PHP:

$query = "SELECT * FROM tblvendors WHERE VendorNo='$VendorNum' AND statuscode='2'";    

并以 json 格式返回值。

 return json_encode($query);

Angularjs

$http.get('pages/modVehiclePayments/getPendingPO.php').then(function (res) {
          $scope.data = res.data; 
});
console.log($scope.data) 

【讨论】:

  • 这就是我现在得到的SyntaxError: missing : after property id $http.get({'pages/modVehiclePayments/getPendingPO.php').success(function(res){
  • Actaully 我正在尝试使用 WHERE 子句选择数据,并且我想使用它不使用 GET 发送变量值来加载数据,我的 url 将像这样 pages/modVehiclePayments/getPendingPO.php ?getvendornumber=VAC-11700000001 但它不工作
  • 停止使用 "success".here 使用这个:$http.get('path').then(function (res){ success msg },function (res){ error msg});
  • 完美,但问题是现在我得到<br /> <b>Notice</b>: Undefined index: getvendornumber in <b>C:\xampp\htdocs\pakmotor\administration\pages \modVehiclePayments\getPendingPO.php</b> on line <b>5</b><br /> [] 没有发送getvendornumber 的值如何转发文本框值
  • 去检查 getPendingPo.php 第 5 行的错误。或者给我看第 5 行代码。
【解决方案2】:

其实我认为你的 php 返回一个“painText”。 尝试使用JSON.parse 获取您的数据或尝试在您的 php 中添加 JSON 标头。

 $scope.list = JSON.parse(data);

【讨论】:

  • 你能登录console.log(data)吗?
  • 我尝试使用 php 文件正确加载 JSON 数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-07
  • 2019-08-05
  • 2019-10-26
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 2022-11-04
相关资源
最近更新 更多