Joh*_*oza 5 javascript node.js cors passport.js axios
尝试使用Google Strategy OAuth2将Vue + Axios应用程序与带有Passport的快速后端结合使用时遇到问题。
我的express服务器已打开,localhost:5000而vue-cli webpack开发服务器已打开localhost:3000。我正在使用webpack proxyTable选项代理从/api到的请求localhost:5000/api。据说它也处理cors。如果我在Vue应用程序中使用常规的超链接,如<a href="/api/auth/google">Auth with Google</a>,它会按预期工作,但是,如果我尝试使用axios,例如:
async authGoogle() {
const response = await axios.get('/api/auth/google')
console.log(response)
}
Run Code Online (Sandbox Code Playgroud)
我从Google api回调中收到此错误:
Failed to load https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A5000%2Fapi%2Fauth%2Fgoogle%2Fcallback&scope=profile%20email&client_id={client_id}:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
这些是api路由:
app.get(
'/api/auth/google',
passport.authenticate('google', { scope: ['profile', 'email'] })
)
app.get(
'/api/auth/google/callback',
passport.authenticate('google'),
function(req, res) {
res.json({ success: true })
}
)
Run Code Online (Sandbox Code Playgroud)
在这种情况下如何使axios正常工作?
| 归档时间: |
|
| 查看次数: |
841 次 |
| 最近记录: |