我有一个php文件,返回带有json标头的json数据
<?php
header('Content-type: application/json;charset=UTF-8');
header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache');
header('Content-Encoding: gzip');
header('Content-Language: en');
header('Pragma: no-cache');
header('Connection: keep-alive');
echo file_get_contents('url');
?>
php文件链接是:http://champtechnet.com/mta/get311.php
我在浏览器中测试了我的链接,并显示了json数据,但是在angularjs中它每次都失败。我以前多次使用angularjs $ http服务,目前正在使用它..
请使用此plunker链接来帮助我:http://plnkr.co/edit/QzkXeCWpUe9wA8dB9W15?p=preview
(function(angular) {
'use strict';
angular.module('httpExample', [])
.controller('FetchController', ['$scope', '$http', '$templateCache',
function($scope, $http, $templateCache) {
$scope.method = 'GET';
$scope.url = 'http://champtechnet.com/mta/get311.php';
$scope.fetch = function() {
$scope.code = null;
$scope.response = null;
$http({
method: $scope.method,
url: $scope.url,
cache: $templateCache
}).
then(function(response) {
$scope.status = response.status;
$scope.data = response.data;
}, function(response) {
$scope.data = response.data || "Request failed";
$scope.status = response.status;
});
};
}
]);
})(window.angular);