James Mitchell
James Mitchell

Reputation: 1

Calling PHP with AngularJS/AJAX/JavaScript

I am very new to PHP and AJAX so please advise if this method is actually even appropriate.

Basically what I am looking to do is populate a .JSON with data from a Microsoft SQL Server. I am using a PHP file to set up the connection which will also populate the .JSON file which can then be read from to populate a ionic list. I am able to read from a .JSON no problem but the issue comes when I call the PHP file.

I am not sure what the best method is for calling it. I've tried AJAX, AngularJS and JavaScript methods but it seems that when it comes to accessing the PHP file they fail but without exception.

This is the code that was able to trigger the .success

angular.module('someApp').controller('someCrtl', function ($scope, $http) {
$http.get("app/dataLink.php").success(

    function phpCall(data) {        
    $scope.info= data;            
}
)});

Note that $scope.info = data; is not what i want to do. What this controller should do is call the php which will populate the .JSON then read from that same .JSON to populate $scope.info

What i can see is that it never actually $http.get the PHP document (I put a breakpoint within it and it was never triggered) but it still continues into .success without exceptions.

I hope that this is enough to go on.

Any other relevant info might be:

Any advice would be much appreciated,

Thanks.

Upvotes: 0

Views: 2928

Answers (2)

harmoniemand
harmoniemand

Reputation: 259

angular.module('someApp').controller('someCrtl', function ($scope, $http) {


    function success(result) {
        console.log(result);
        $scope.info = result;
    };

    function error(result) {
        alert("error");
    };

    $http.get("app/dataLink.php").then(success, error);
});

Upvotes: 1

eselskas
eselskas

Reputation: 817

From AngularJs doc:

The $http legacy promise methods success and error have been deprecated.

AngularJS: API: $http

Replace your success block with something like:

.then(function(response) {
     console.log(response);
}

See if you get any response when you inspect. Also watch your "network" tab if you use chrome, it will show if the call was successful (200) or something went wrong (500) and debug the problem that way.

Upvotes: 0

Related Questions