javascript - Fetch data from database in php using angular js $http.get -
i using angular js script fetch data external php file encoded in json in html page. have used $http.get(page2.php) method fetch json encoded array written in file. problem not showing output blank screen dont know doing wrong.
here's page1.html
<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script> var app = angular.module('myapp',[]); app.controller('myctrl',function($scope,$http){ $http.get('page2.php').success(function(response){ $scope.names = response; }); }); </script> </head> <body> <div> <table ng-app="myapp" ng-controler="myctrl" > <tr ng-repeat="x in names"> <td>{{ x.id }}</td> <td>{{ x.name }}</td> <td>{{ x.age }}</td> </tr> </table> </div> </body> </html> here's page2.php
<?php $con = mysqli_connect('localhost','root','','practice'); if(!$con){ die("couldnt connect".mysqli_error); } $query = "select * records"; $result = $con->query($query); $r = array(); if( $result->num_rows>0){ while($row = $result->fetch_assoc()){ $r[] = $row; } } $res = htmlspecialchars(json_encode($r)); echo $res; ?> i can't figure out doing wrong.
if you're sure you're getting data query, need send json. add before echo $res; line:
header('content-type: application/json');
Comments
Post a Comment