2016-02-19 61 views
0

嗨iam试图使用angularjs从数据库中获取数据,但它不显示任何数据。我刚开始学习AngularJs.Can任何人都可以帮助我。这里是我的代码使用angularjs和JSON从数据库中获取数据

income.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); 
     });    
}; 
}); 

incomesources.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::";    
    } 
} 
+0

这里u需要在成功添加更多的参数数据FN –

+0

我需要添加 – Nagu

+0

.success(功能(状态,报头数据){ 控制台哪个参数。 log(“Response data:”+ JSON.stringify(data)); –

回答

0

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

的WHERE SELECT语句子句寻找电子邮件=“$ USEREMAILID”

我想你想$ USEREMAILID的内容。因此,尝试改变

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

编辑 因为你的PHP文件是否正常工作,并且你在那你可以看到在控制台返回值以上的评论中提到,让我们看看你的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.user之外的$ scope.getIncomeSource函数并将其初始化为一个数组。例如

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

$scope.user = []; // this 

$scope.getIncomeSource = function(){ 

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

希望这有助于

+0

不,它是工作它是在控制台打印数据 – Nagu

+0

正如我也试过这个,但它不工作 – Nagu

+0

对不起,我认为PHP的返回是一个数组。它只是一个json对象。您可以请将该行更改为$ scope.user = {}; –