gpt4 book ai didi

node.js - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。因此不允许访问 Origin 'localhost:8080'

转载 作者:太空宇宙 更新时间:2023-11-04 02:47:46 25 4
gpt4 key购买 nike

我在论坛上多次看到这个问题,但使用这些解决方案并没有帮助。

我正在使用 PassportJS 构建 MEAN 堆栈应用程序以允许使用 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 不会发送 header-origin,并且由于它是重定向,因此 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 header (不是您自己的 Node 服务器)。你无法回避这个问题。所以你可以使用简单的墨水<a href="/auth/twitter"></a>而不是 $http.get 或 $window.location.href 。请注意,在这种情况下您不会应用 http 拦截器,因此您可能需要传递一些查询参数。

关于node.js - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。因此不允许访问 Origin 'localhost:8080',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25148785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com