Javascript 使用$http.get检索单个记录
我在PHP文件中有几个函数,用于从服务器数据库写入和请求数据。对这些php函数的请求来自一个js文件。我的第一个写入服务器的函数可以工作,但是我在控制台中遇到了一个障碍,没有错误,无法继续检索单个记录 目前,我相信我已经正确地提取了数据,正如我在控制台日志中看到的那样,但是数据似乎没有分配给变量 我的PHP文件Javascript 使用$http.get检索单个记录,javascript,php,sql,angularjs,Javascript,Php,Sql,Angularjs,我在PHP文件中有几个函数,用于从服务器数据库写入和请求数据。对这些php函数的请求来自一个js文件。我的第一个写入服务器的函数可以工作,但是我在控制台中遇到了一个障碍,没有错误,无法继续检索单个记录 目前,我相信我已经正确地提取了数据,正如我在控制台日志中看到的那样,但是数据似乎没有分配给变量 我的PHP文件 switch($_GET['action']){ case 'writeInvoice': writeInvoice(); break; case
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/angular代码
$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");
});
实际上,我刚才发现了这一点,但是它似乎并没有仍然赋值。好的,您需要将结果赋值给回调中的
$scope.list
。好的,我已经这样做了,但仍然没有。我想知道调用CustomModal是否与时间有关?您是否也在回调中添加了它?我添加了。当我有list={{list}时,没有显示任何内容。我还尝试分离CustomModal调用,但这也不是问题所在