如何在AngularJs中启用CORS我已经为Flickr照片搜索API使用JavaScript创建了一个演示。现在我要把它转换成安古拉杰。我已经在网上搜索,并发现了下面的配置。配置:myApp.config(function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];});服务:myApp.service('dataService', function($http) {
delete $http.defaults.headers.common['X-Requested-With'];
this.flickrPhotoSearch = function() {
return $http({
method: 'GET',
url: 'http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=3f807259749363aaa29c76012fa93945&tags=
india&format=json&callback=?',
dataType: 'jsonp',
headers: {'Authorization': 'Token token=xxxxYYYYZzzz'}
});
}});主计长:myApp.controller('flickrController', function($scope, dataService) {
$scope.data = null;
dataService.flickrPhotoSearch().then(function(dataResponse) {
$scope.data = dataResponse;
console.log($scope.data);
});
});但我还是犯了同样的错误。以下是我尝试过的一些链接:XMLHttpRequest无法加载URL。访问不允许的原产地-控制-允许-原产地http:/samurails.com/教程/cors-具有角-js-和-sinatra/编辑:根据@quentin的建议,我在node.js中创建了一个代理服务器:var http = require('http');var url = require('url');var fs = require('fs');var server;server = http.createServer(function (req, res) {
// your normal server code
var path = url.parse(req.url).pathname;
fs.readFile(__dirname + path, function (err, data) {
if (err) {
return send404(res);
}
res.writeHead(200, {'Content-Type':path == 'json.js' ? 'text/javascript' : 'text/html'});
res.write(data, 'utf8');
res.end();
});}),server.listen(8001);//using express to load customizes static filesvar express = require("express"),最终编辑我删除了授权头headers: {'Authorization': 'Token token=xxxxYYYYZzzz'}一切都很顺利。我得到了我想要的。谢谢大家参与这个问题
添加回答
举报
0/150
提交
取消