Javascript AngularJS HTTP头
我有一些问题。来自外部url的AngularJS应用程序数据。这是我的密码。如何写入授权头。响应为HTTP状态401Javascript AngularJS HTTP头,javascript,angularjs,http,header,Javascript,Angularjs,Http,Header,我有一些问题。来自外部url的AngularJS应用程序数据。这是我的密码。如何写入授权头。响应为HTTP状态401 app.controller('AddressController', function($scope, $http){ $http.defaults.headers.common.Authorization = 'username password'; $http.get('http://URL/resources/cities?sortBy=cityId&sortOr
app.controller('AddressController', function($scope, $http){
$http.defaults.headers.common.Authorization = 'username password';
$http.get('http://URL/resources/cities?sortBy=cityId&sortOrder=asc&offset=0&limit=100').
success(function(data, status, headers, config){
$scope.posts = data;
}).
error(function(data, status, headers, config){
});
});
我过去也遇到过类似的问题,更准确地说,是CORS问题。 您可以在以下网站上阅读有关跨来源资源共享(CORS)的更多信息:
- 修复Apache2.config文件
- 在AngularJS控制器上设置权限
myApp.controller("myController", ["$scope","$http",function($scope, $http){
var controller = this;
$http.get('http://localhost/main.json').success(function(dataForViews){
controller.data = dataForViews;
})
}]);
在我的Html视图中我只是用如下语法请求数据:
<p> {{myController.dataForViews.Name}} </p>
{{myController.dataForViews.Name}
现在,为了允许访问,我将此代码放在我的Apache2.config上:
<VirtualHost *:80>
ServerAdmin yourName@yourName
#Giving permissions
Header set Access-Control-Allow-Origin "*"
############################ Main Page ###########################
Alias /main.json /var/www/html/myApp/data/mainPages/mainJSON.json
#When localhost/myApp, you get the application that runs on localhost:9000
#with its jsons
<Location /myApp>
ProxyPass http://localhost:9000/
ProxyPassReverse http://localhost:9000/
</Location>
#Same thing for scripts, css, etc, like:
<Location /scripts>
ProxyPass http://localhost:9000/scripts/
ProxyPassReverse http://localhost:9000/scripts/
</Location>
</VirtualHost>
服务器管理员yourName@yourName
#给予许可
标题集访问控制允许原点“*”
############################主页###########################
别名/main.json/var/www/html/myApp/data/mainPages/mainJSON.json
#当使用localhost/myApp时,您将获得在localhost:9000上运行的应用程序
#带着它的JSON
ProxyPasshttp://localhost:9000/
ProxyPassReversehttp://localhost:9000/
#脚本、css等也一样,比如:
ProxyPasshttp://localhost:9000/scripts/
ProxyPassReversehttp://localhost:9000/scripts/
我能够像这样访问我的Apache2服务器上的资源。
现在,你可以像我一样解决,或者,如果你有一些错误,你可能需要看看
如果您希望以AngularJS的方式解决此问题,请查看:
- 这是@Quentin写的
- 这是@CapK写的
- 以及其他相关问答
希望我能帮上忙。您使用的是哪种外部服务?我是说类似Apache的东西,你把它整理好了吗?