Search code examples
phphttpangularjsangularjs-service

AngularJs http get to PHP script returns the php code


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!


Solution

  • 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.