使用$http.get检索单个记录

Using $http.get to retrieve a single record

本文关键字:单个 记录 检索 get http 使用      更新时间:2023-09-26

我在一个PHP文件中有几个函数,用于从服务器数据库中写入和请求数据。对这些php函数的请求来自一个js文件。我的第一个向服务器写入的功能可以工作,但我的控制台中没有任何错误,无法继续检索单个记录。

目前,我相信我已经正确地提取了数据,正如我在控制台日志中看到的那样,但数据似乎没有分配给变量。

我的PHP文件

    switch($_GET['action']){
case 'writeInvoice':
        writeInvoice();
        break;
case 'fetchInvoice':
        fetchInvoice($_GET['num']);
        break;
}
function fetchInvoice($num){
$fqry = "SELECT customerName,
                dealerName,
                billTo,
                billStart,
                contractTerms,
                item,
                itemPrice,
                quantity,
                sharePercent,
                cost
                FROM invoice
                WHERE idInvoice = '{$num}'";

$qry = new Query($fqry,__LINE__,__FILE__);
$result = json_encode($qry->fetch_array());
print_r($result);

return $result;
}

我的js/角码

    $scope.editInvoice  = function($param){
    $http.get('DealerRec/writeInvoice.php?num='+$param+'&action=fetchInvoice', $scope.list)
        .success(function(data,status,headers,config){console.log("Data Taken"); console.log(data);
         fetchData = data; 
          $scope.list.idInvoice     = $param;
    $scope.list.customerName  = fetchData.customerName;
    $scope.list.dealerName    = fetchData.dealerName;
    $scope.list.billTo        = fetchData.billTo;
    $scope.list.billStart     = fetchData.billStart;
    $scope.list.item          = fetchData.item;
    $scope.list.price         = fetchData.price;
    $scope.list.qty           = fetchData.qty;
    $scope.list.contractTerms = fetchData.contractTerms;
    $scope.list.per           = fetchData.per;
    $scope.list.cost          = fetchData.cost;
 })
        .error(function(data,status,headers,config){ console.log("Data Not Taken"); });

    CustomModal.setOption(2);
};

我的目标是通过发票ID#从数据库中检索发票,并存储我可以查看和写入的字段。

我的控制台显示了这一点,当我调用函数时,比如说,条目12

Data Taken
(
    [num] => 12
    [action] => fetchInvoice
 )
                                          {"customerName":"SomeCustomer","dealerName":"SomeDealer","billTo":"Customer","billStart":"2015-06-10","contractTerms":"Billing Software    Terms","item":"SomeItem","itemPrice":"150.00","quantity":"20","sharePercent":"75","cost":"15.00"}

当函数执行时,它在最后一行打开一个模态,其中数据是设置/可编辑的。

以下部分肯定是错误的:

fetchData = $http.get('DealerRec/writeInvoice.php?num='+$param+'&action=fetchInvoice', $scope.list)
    .success(function(data,status,headers,config){console.log("Data Taken"); console.log(data); })
    .error(function(data,status,headers,config){console.log("Data Not Taken"); });

不能像这样将$http服务的结果分配给fetchData。你需要在成功回调中这样做:

$http.get('DealerRec/writeInvoice.php?num='+$param+'&action=fetchInvoice',$scope.list)
    .success(function(data,status,headers,config){
        console.log("Data Taken"); 
        console.log(data); 
        fetchData = data; // this is what you have to do
    })
    .error(function(data,status,headers,config){ 
        console.log("Data Not Taken"); 
    });