[英]AngularJS $http.get return error
My first time with http request. 我第一次使用http请求。 I use Angular for it.
我使用Angular。 Server works normal - it's public news API.
服务器正常运行-它是公共新闻API。 I need to get JSON file by URL like "hostname.com/article/2014/06/10/123?api-key=1234567890".
我需要通过“ hostname.com/article/2014/06/10/123?api-key=1234567890”之类的URL获取JSON文件。
function Ctrl($scope, $http, $templateCache) {
//some code there
$scope.load_article = function( patch ) {
$http.get(patch + "?" + $scope.apikey)
.success(function(response){
result = angular.fromJson(response.data);
$scope.article = result;
}).error(function(response) {
$scope.article = "error "+ response.status;
});
};
}
But when i call load_article() tracer shows me that result: 但是当我调用load_article()时,跟踪器向我显示了该结果:
Method: OPTIONS;
Status: 596 OK;
Type: text/xml;
and "error undefined" into $scope.article. 和“错误未定义”到$ scope.article中。
Where is my fault? 我的错在哪里
Upd: UPD:
$http.jsonp(patch + "?" + $scope.apikey).success(function(data)){...}
Will be better for get JSON file. 对于获取JSON文件会更好。
JSONP usually requires you to send a callback function in your request (you should look up the documentation for the api). JSONP通常要求您在请求中发送回调函数(您应该查看api的文档)。 If you'd tell us what public news API you're using, someone might help.
如果您要告诉我们您使用的是什么公共新闻API,可能会有人提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.