我正试图从一个吐出JSON的php脚本中获取一个echo。
<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
我的角度代码中有以下内容:"严格使用";
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;
});
然后在我的服务中,我有:
HttpRequestService.async().then(function(data) {
console.log('data is : ' + data);
});
但这篇文章吐出了整个php脚本,而不仅仅是echo数据:
console :
data is : <?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
你知道为什么会这样吗?谢谢
检查在浏览器中打开test.php
时返回的响应。
您的php没有被编译。也许您没有在web服务器中运行php模块。
这是一个工厂,为了命名约定,您可能需要将其重命名为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;
});