如何在AngularJs中启用CORS
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); }); });
编辑:
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'}
叮当猫咪
相关分类