Reputation: 4388
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!
Upvotes: 1
Views: 4225
Reputation: 11137
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.
Upvotes: 3
Reputation: 9614
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;
});
Upvotes: 1