簡體   English   中英

所請求的資源上沒有“ Access-Control-Allow-Origin”標頭。 因此,不允許訪問源“ localhost:8080”

[英]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.

相關問題 Mean.io在請求的資源上不存在“ Access-Control-Allow-Origin”標頭。 因此,不允許訪問來源“ http:// localhost:3000” Socket.io沒有&#39;Access-Control-Allow-Origin&#39;標頭出現在請求的資源上。 因此不允許Origin&#39;http:// localhost&#39;訪問 請求的資源上不存在“Access-Control-Allow-Origin”標頭。 因此不允許Origin&#39;http:// localhost:8100&#39;訪問 XMLHttpRequest無法加載。 請求的資源上不存在“Access-Control-Allow-Origin”標頭。 因此不允許原點訪問 請求的資源上不存在“Access-Control-Allow-Origin”標頭。 Origin &#39;https://myappurl.com 因此不允許訪問 [Passportjs][Angular5] 請求的資源上不存在“Access-Control-Allow-Origin”標頭。 因此不允許訪問原點 &#39;null&#39; **請求的資源上不存在“Access-Control-Allow-Origin”header。** XMLHttpRequest無法加載請求的資源上不存在“Access-Control-Allow-Origin”標頭。 Origin'http:// localhost:3000'Google maps 如何修復“http://localhost:3000”已被 CORS 策略阻止:請求的資源上不存在“Access-Control-Allow-Origin”header。 Access-Control-Allow-Origin&#39;標頭存在於請求的資源上。 錯誤
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM