I am wondering why my AngularJS http service is always returning error with 
status -1.

I have php code and run php code as localhost/ajax.php can retrieve data 
from database. So php is working fine. 

The retrieved data is as follow.





{"id":"5","Name":"Tom","Gender":"Male","City":"New York"},

{"id":"6","Name":"Pam","Gender":"Male","City":"Los Angeles"},






{"id":"12","Name":"Tony","Gender":"Male","City":"Hong Kong"},



{"id":"15","Name":"Tommy","Gender":"Male","City":"New Jersy"}]

Those php files are called from AngularJS http service, but that call 
returns always error with status -1.

I looked at all the resources but got no clues. My database is setup at 
localhost using Sql.

My queries are what could be the error sources those are making the http 
service return with error status -1.

My codes are as follows.


 require 'connect.php';

 $connect = connect();

 // Get the data
 $students = array();
 $sql = "SELECT id, Name, Gender, City FROM tblStudents";

 if($result = mysqli_query($connect,$sql))

 $count = mysqli_num_rows($result);
 $cr = 0;
 while($row = mysqli_fetch_assoc($result))
 $students[$cr]['id'] = $row['id'];
 $students[$cr]['Name'] = $row['Name'];
 $students[$cr]['Gender'] = $row['Gender'];
 $students[$cr]['City'] = $row['City']; 

 $json = json_encode($students);
 echo $json;



    // db credentials 
    define('DB_HOST', 'localhost'); 
    // Connect with the database. 
    function connect() { 
         $connect = mysqli_connect(DB_HOST ,DB_USER ,DB_PASS ,DB_NAME);    
         if (mysqli_connect_errno($connect)) { 
              die("Failed to connect:" . mysqli_connect_error()); } 
         mysqli_set_charset($connect, "utf8"); return $connect; } 


var app = angular.module("Demo", ["ngRoute"]) 
                   .when("/home", { templateUrl:"Templates/home.html", 
controller:"homeController" }) 
                   .when("/courses", { templateUrl:"Templates/courses.html", 
controller:"coursesController" }) 
                   .when("/students", { templateUrl:"Templates/students.html", 
controller:"studentsController" }) }) 
                 .controller("homeController", function($scope){ $scope.message 
= "Home Page"; }) 
                 .controller("coursesController", function($scope){ 
$scope.courses = ["C#", "VB.NET", "SQL Server", "ASP.NET"]; }) 
                 .controller("studentsController", function ($scope, $http) { 
$scope.students; $http({ method: 'GET', url: 'api/ajax.php' })
                 .then(function (response) { $scope.students = response.data; },
                  function (response) { 
console.log(response.data,response.status); }); });


<!DOCTYPE html> 
<!-- To change this license header, choose License Headers in Project 
Properties. To change this template file, choose Tools | Templates and open the 
template in the editor. -->
<html ng-app="Demo">
    <title>TODO supply a title</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <script src="Scripts/angular.min.js" type="text/javascript"></script> 
     <script src="Scripts/angular-route.min.js" 
     <script src="Scripts/Script.js" type="text/javascript"></script> 
     <link href="Styles.css" rel="stylesheet" type="text/css"/> 
<body > 
     <table style="font-family: Arial"> 
           <td colspan="2" class="header"> <h1> WebSite Header </h1> </td> 
       <tr ng-controller="studentsController"> 
            <td class="leftMenu"> <a href="#/home">Home</a> <a 
href="#/courses">Courses</a> <a href="#/students">Students</a> </td> 
            <td class="mainContent"> <ng-view></ng-view> </td> 
         <td colspan="2" class="footer"> <b>Website Footer</b> </td> 

You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to angular+unsubscr...@googlegroups.com.
To post to this group, send email to angular@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to