1

I've done quite a lot of posting with Angular, however I've never received a response and I'm having trouble with it.

PHP

<?php
    return "testing";
?>

I'm actually unsure whether I should use return or echo here. I've tried both. And the Angular documentation doesn't provide much insight into the server side scripts.

This is in my Controller:

this.acquireImages = function() {
        $http.get("resources/php/gallery.php").success(function(response){
            this.returnData = response.data;
        });
    };
    this.acquireImages();

I've defined returnData before just to make sure it wasn't something simple like not displaying the correct variable, etc.

I've seen the documentation here and I'm following as well as I can, but I can't seem to get this simple example to work.

2 Answers 2

2

On the PHP side, you have to echo and not return the data.

If you use the return statement, you will return some data at the server level.

So you need to write your data.

PHP

<?php 

    echo 'hello world !';


 ?>

In the success callback, the response is our raw data.

But, you should wrap your api call into a service, then you will get :

Controller

(function(){

    function Controller(Service){


        //Our callback
        function print(data){
            console.log(data);
        }

        //Call service with our callback
        Service.get(print);

    }

    angular
    .module('app')
    .controller('Ctrl', Controller);

})();

Service

(function(){

    function service($http){

        this.get = function(callback){
            $http.get('server.php').success(function(data){
                callback(data);
            });
        }

    }

    angular
        .module('app')
        .service('Service', service);

})();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. I have a couple sub-questions. Why does the controller need the $http object listed as a dependency if the service contains a dependency for the $http object? Why are we wrapping this in a service anyway? I only need the response for one controller...don't you only use services when you want to share data between multiple controllers? And last, I'm at a loss as to the purpose and function of the "callback" contained in the this.get function. And why are we wrapping everything in an anonymous function?
The controller doesn't need the $http object, it was a mistake. Then, why use Service ? We use it in order to make the code more readable and modular. But, it's right, you can share data between controllers with services, because all angular services are singletons. We use callback function in the this.get to recover properly the data in our controller, and to ensure that the data will be set.
Ahh. I figured it our. We were passing a function as the dependency of another function. So, I just realized that the problem I was having originally was that I was declaring a variable in a lower scope. How bad of a practice is it to just do it all without a service?
In your case, it's true, you can do it without service, because the route will not change, go ahead and use the $http directly, without a wrapper. But wrapping into services, allows you to use parameters for your call. So, you would have only one place to change the route for example. As i said, decision to build services, and making factors here should be reusability, readability. You can also look about $resource.
1

Inside $http.get function this mean something else than outside. You should do something like that:

var that = this;
$http.get("resources/php/gallery.php").success(function(response){
    that.returnData = response.data;
});

Also in PHP you should do echo 'something', becouse in Angular you get the same content as your browser when you visit this URL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.