0

I am trying to make a http request to my php file. But it returns me the following error

Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"localhost/sample.php","headers":{"Accept":"application/json, text/plain, /"}},"statusText":"","xhrStatus":"error"}

My angular request is

var some = angular.module("first_module", [])
            .controller("first_controller", function($scope, $http, $log) {
    $http.get('localhost/sample.php').then(function(response) {
                    $scope.students = response;
                    $log.info();
        });
});

My PHP file is

<?php
$var2 = new stdClass();
$var2->name = 'john';
$var2->class = '7';
$var2->fee = '45000';
$var2->point = '5';
$response[0] = $var2;
$var2->name = 'ahmed';
$var2->class = '7';
$var2->fee = '55600';
$var2->point = '2';
$response[1] = $var2;
echo json_encode($response);
?>

I am unable to understand the error even. Thanks in advance

UPDATE: on adding an another function for failure and print the log in console I receive the following error

var some = angular.module("first_module", [])
            .controller("first_controller", function($scope, $http, $log) {
    $http.get('localhost/sample.php').then(function(response) {
                    $scope.students = response;
                    $log.info();
        }, function(err) {
                      console.log($log.info);
         });
});

This is the error I receive enter image description here

rammanoj
  • 479
  • 8
  • 26

1 Answers1

0

I think you should take an safer approach:

Take your URL an test it in Browser,

Http(s)://localhost/sample.php

if the Browser returns your result and everything is fine then use the full URL in your code. (With this test you are testing that your server is running, and the .php file returns the expected values as json.)

$http.get('http://localhost/sample.php') 

Or if you are sure on which relative path your files are. Like executing all files in same folder.

$http.get('sample.php')
Michael
  • 556
  • 2
  • 8
  • My sample.php is on apache server on ubuntu system i.e in directory /var/www/html/ but my angular file is in Desktop. I can't find any reason for the error ? However thanks for answer. And the url http://localhost/sample.php is printing the json data perfectly. – rammanoj Jun 03 '18 at 18:06
  • You need to run angular also on the Server. You can get domain-cross-script errors. Try to put your angular app in a subfolder .../html/app and then go with localhost/app/angular.html – Michael Jun 03 '18 at 20:08