[英]Unable to make a $http.get request to a local JSON file using Angular
[英]Unable to access Json using http.get . status is coming out is -1
我是AngularJS的新手。 在我的代码中,我试图使用$http.get
访问json数据,但是它将出错并且状态返回为-1。
怎么了?
RecordApp.factory('recordaccess', ['$http', function($http) {
$http.get('http://someurlforjson')
.success(function(data) {
return data;
})
.error(function(data, status, headers, config) {
alert("Error occurred. Status: " + status);
});
}]);
$ http旧式的Promise方法success
和error
已被弃用。 使用标准的then
方法代替:
RecordApp.factory('recordaccess', ['$http', function($http) {
return $http.get('http://someurlforjson')
.then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
return response.data;
},
function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
alert("Error occurred. Status: " + reponse.status + " - " + response.statusText);
});
}]);
您可以在Angular Docs中找到更多信息。
如果这没有帮助,则JSON路径必定有问题(该路径不正确或不允许您访问它,例如CORS)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.