401错误与发布请求Stormpath Express + React + Node + Gulp

尝试login任何用户时,出现以下POST错误结果:

XMLHttpRequest cannot load https://api.stormpath.com/v1/applications/[APP_HREF]/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401. 

在我的server.js文件中,我提出了以下内容:

 var express = require('express'); var stormpath = require('express-stormpath'); var cors = require('cors'); var app = express(); app.use(stormpath.init(app, { apiKey: { id: 'xyz', secret: 'abc' // Using the unsafe inline option for example purposes }, application: { href: `[APP_HREF]` }, web: { produces: ['application/json'] }, debug: 'info' })); app.use(cors({ origin: 'http://localhost:8080', credentials: true })); app.post('/', stormpath.loginRequired, function (req, res) { function writeError(message) { res.status(400); res.json({ message: message, status: 400 }); res.end(); } }); app.on('stormpath.ready', function () { var server = app.listen(process.env.PORT, function() { try { process.send('CONNECTED'); } catch(e) {} }); }); 

在我的login.jsx文件中,我包含了:

 login: function(e) { e.preventDefault(); e.stopPropagation(); this.serverRequest = $.post('https://api.stormpath.com/v1/applications/[APP_HREF]/login', { "username": document.getElementById("email").value, "password": document.getElementById("pass").value }, function (result) { console.log(result); }); 

我也保存了我的stormpath.yml文件。

我没有使用React-Stormpath,因为我已经为login和注册创build了我的视图。 它看起来像我只需要访问与Stormpath REST API,但我不知道我需要添加以获得API密钥validation。

在实际的login.jsx文件中,是否需要发送ID:SECRET对以及POST请求的一部分?

我在你的login.jsx看到你正在试图直接发布到Stormpath REST API,但不幸的是这还不行。 相反,您将发送到您的快递服务器,反过来它将与Stormpath沟通。

您的Express服务器上已经有了express-stormpath ,所以您只需要将login表单发送到/login ,Stormpath就会处理其他问题:)

让我们知道,如果你遇到任何问题! 仅供参考,我们将很快添加“无服务器”function,您可以在此处查看: http : //ideas.stormpath.com/ideas/IAM-I-59