1

I am trying to grab an echo from a php script that spits out JSON.

<?php
    $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
    echo json_encode($arr);
?>

What I have in my angular code is the following: 'use strict';

app.factory('HttpRequestService', function($http) {
  var HttpRequestService = {
    async: function() {

      var promise = $http.get('/test.php').then(function (response) {
        return response.data;
      });
      return promise;
    },
    load: function() {
        console.log('helloo');
    }
  };
  return HttpRequestService;
});

And then in my service I have :

HttpRequestService.async().then(function(data) {
    console.log('data is : ' + data);
});

but this literary spits the whole php script and not just the echo data:

console :
data is : <?php
    $arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
    echo json_encode($arr);
?> 

Any ideas why this is happening? Thanks!

2
  • If you navigate to the php page in browser what happens? Commented Jul 30, 2013 at 17:47
  • 1
    What happens when you hit the test.php page with your browser? What's the response that comes back. It looks like your php isn't being compiled. Perhaps you didn't run the php module in your web server. Commented Jul 30, 2013 at 17:47

2 Answers 2

3

Check the response that comes back when you open test.php in your browser.

Your php isn't being compiled. Perhaps you didn't run the php module in your web server.

Sign up to request clarification or add additional context in comments.

Comments

1

This is a factory, for naming convention sake, you might want to rename it HttpRequestFactory

app.factory('HttpRequestService', function($http, $q) {
  var HttpRequestService = {
    async: function() {
      var deferred = $q.defer();
      $http.get('/test.php')
         .success(function (data, status, headers, config) {
            deferred.resolve(data);
         })
         .error(function(data, status, headers, config){
            deferred.reject("An error occured");
         });
      return deferred.promise;
    }
  };
  return HttpRequestService;
});

1 Comment

Thank you for the tip. I completely overlooked that part. I feel so newbish now lol.

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.