[英]Mean.io No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access
[英]No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:8080' is therefore not allowed access
我已經多次在論壇上看到這個問題,但是解決這些問題並沒有幫助。
我正在使用PassportJS構建MEAN Stack應用程序,以允許使用Twitter登錄。
angular.module('HomeCtrl', []).controller('HomeController', function($scope,$http) {
$scope.tagline = 'To the moon and back!';
$scope.twit = function() {
console.log("twitter button clicked");
$http.get("/auth/twitter")
.success(function (data) {
console.log(data);
})
//$window.location.href = '/auth/twitter';
}});
我在服務器中的route.js有
app.get('/auth/twitter', passport.authenticate('twitter', { scope : 'email' }));
現在,當Twitter重定向到應用程序時,由於CORS問題,服務器重定向無法正常工作,我能夠理解。 要解決這個問題
我嘗試了以下
app.all('/*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:8080");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
next();
});
但這仍然行不通。 在網上閱讀更多內容后,我的理解是twitter不會發送標頭來源,並且由於它是重定向的,因此node.js服務器無法控制它收到的響應,該響應可以發送給瀏覽器。
在此階段,我不確定如何進行。 請指教
根據評論:這是Twitter回調的響應
Request URL:https://api.twitter.com/oauth/authenticate?oauth_token=l8OKcAvqr3QLrlCroweGgByvvhXfSmIiqhvRgGqML6c
Request Headers
Provisional headers are shown
Accept:application/json, text/plain, */*
Origin:http://localhost:8080
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Query String Parametersview sourceview URL encoded
oauth_token:l8OKcAvqr3QLrlCroweGgByvvhXfSmIiqhvRgGqML6c
這里的問題是twitter沒有設置CORS標頭(不是您自己的節點服務器)。 你無法解決這個問題。 因此,您可以使用簡單的墨水<a href="/auth/twitter"></a>
代替$ http.get或$window.location.href
。 請注意,在這種情況下您將不會應用HTTP攔截器,因此您可能需要傳遞一些查詢參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.