【问题标题】:fetching the data from database using angularjs and JSON使用 angularjs 和 JSON 从数据库中获取数据
【发布时间】:2016-05-31 16:01:41
【问题描述】:

嗨,我正在尝试使用 angularjs 从数据库中获取数据,但它没有显示任何数据。我刚开始学习 AngularJs。任何人都可以帮助我。这是我的代码

收入.html

<div class="container jumbotron"  ng-init= "getIncomeSource()" ng-controller="IncomeSourcesController" id="homejumbotron">
<table>
    <thead>
        <th>Name of the Employer</th>
        <th>Income From Salary</th>
        <th>TDS Deducted</th>
        <th></th>
    </thead>
    <tbody>
        <tr ng-repeat="x in incomesources" >

    <td>{{x.company_name}}</td>
    <td>{{x.user_income}}</td>
    <td>{{x.tax_deducted_salary}}</td>


    </tbody>

js

var app = angular.module('accountantApp', []);
app.controller('IncomeSourcesController', function($scope,$http) {
console.log("inside homecontroller:::");    
$scope.getIncomeSource = function(){
$scope.user = "";
console.log("Inside getPersonalInfo::::::");    
$http({
        method  : 'POST',
         url        : '../model/incomesources.php',           
        headers : {
                    'Content-Type': 'application/json'
                  },
              data      : {"action": "GetIncomeSources"}
       })
       .success(function( data,status, headers) {
        console.log("Response data:"+ JSON.stringify(data));
        if (data.success != undefined && data.success != '')
        {
          $scope.user = data;
        }
        })
        .error(function(data, status, headers) {
        alert("Error occured while retrieving:"+status);
        console.log("Error data::::"+ data);
        console.log("status::::"+ status);
        });             
};  
});

收入来源.php

function getIncomeSources()
{
session_start();
 $userInfo = $_SESSION['USER'];
 $userEmailid= $userInfo-> getEmailid();
 $res="SELECT *  FROM user_salary_details  WHERE email ='$userEmailid'";
 $result=mysql_query($res);            
 if ($row = mysql_fetch_assoc($result))
  {
     $companyname  = $row["company_name"];
     $userincome = $row["user_income"];
     $employetype   = $row["employe_type"];
     $tan     = $row["tan_employer"];
     $tax = $row["tax_deducted_salary"];
     $address = $row["address"];
     $state = $row["state"];
     $city=$row["city"];
     $pin=$row["pincode"];
     $data = array(
                    "success"   =>  "success",
                "companyname"   =>  $companyname,
                    "userincome"    =>  $userincome,
                "employetype"   =>  $employetype,
                "tan"   =>  $tan,
                    "tax"   =>  $tax,
                    "address"    =>      $address,
                    "state"   =>      $state,
                     "city" =>$city,
                     "pin"=>$pin,
              );

     echo json_encode($data);         
     }else{
        echo "No record exists for this user::";            
     }   
}

【问题讨论】:

  • 这里需要再添加一个参数数据成功fn
  • 我需要添加哪个参数
  • .success(function(status, headers, data) { console.log("响应数据:"+ JSON.stringify(data));
  • console.log("Response data:"+ JSON.stringify(data) 这里你使用了数据但是数据没有在任何地方定义
  • 控制台显示什么?

标签: javascript php mysql angularjs json


【解决方案1】:

您的incomesources.php 最初是否有效?

select 语句中的 WHERE 子句正在寻找 email='$userEmailid'

我想你想要 $userEmailid 的内容。所以尝试更改为

$res="SELECT *  FROM user_salary_details  WHERE email ='". $userEmailid ."'";

编辑 由于您的 php 文件正在运行,并且您在上面的 cmets 中提到您可以在控制台中看到返回值,所以让我们看看您的 html 和 js 代码。

在你的html中请改成

<tr ng-repeat="x in user" >

    <td>{{x.companyname}}</td>
    <td>{{x.userincome}}</td>
    <td>{{x.tax}}</td>

{{x.something}} 中的属性基于 php 返回属性,而不是来自数据库列名。

在您的 js 文件中,尝试在 $scope.getIncomeSource 函数之外初始化 $scope.user 并将其初始化为数组。例如

console.log("inside homecontroller:::");   

$scope.user = []; // this

$scope.getIncomeSource = function(){

console.log("Inside getPersonalInfo::::::");    
$http({

希望有帮助

【讨论】:

  • 对不起,我以为php的返回是一个数组。它只是一个 json 对象。你能把这行改成 $scope.user = {};
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
相关资源
最近更新 更多