发布请求时出现401错误Stormpath Express + React + Node + Gulp [英] 401 Error with post request Stormpath Express + React + Node + Gulp

查看:228
本文介绍了发布请求时出现401错误Stormpath Express + React + Node + Gulp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试登录任何用户时,将出现以下POST错误:

When attempting to login any user, the following POST error results:

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文件中,放置了以下内容:

In my server.js file, I have put the following:

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文件中,我包括:

In my login.jsx file, I have included:

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文件.

I also have saved my stormpath.yml file.

我没有使用React-Stormpath,因为我已经为登录和注册创建了视图.看来我只需要使用Stormpath访问REST api,但是我不确定我需要添加什么来使API密钥得到验证.

I'm not using React-Stormpath because I already created my views for Login and Registering. It looks like I only need to access the REST api with Stormpath, but I'm not sure what I need to add in order to get the API key validated.

在实际的login.jsx文件上,我是否必须发送ID:SECRET对以及POST请求的一部分?

On the actual login.jsx file, would I have to send the ID:SECRET pair as well as part of the POST request?

推荐答案

我看到您正在login.jsx中尝试将其直接发布到Stormpath REST API,但不幸的是,这还不可能.相反,您可以将帖子发布到Express服务器,然后它将与Stormpath通信.

I see that in your login.jsx you are trying to post directly to the Stormpath REST API, but unfortunately that isn't possible yet. Instead you will make the post to your Express server, and in turn it will communicate with Stormpath.

您的Express服务器中已经有express-stormpath,因此您只需将登录表单发布到/login,Stormpath将负责其余的工作:)

You already have express-stormpath in your Express server, so you just need to post your login form to /login and Stormpath will take care of the rest :)

让我们知道您是否遇到任何问题!仅供参考,我们将很快添加无服务器"功能,您可以在此处进行以下操作: http://ideas.stormpath.com/ideas/IAM-I-59

Let us know if you run into any issues! FYI, we will be adding a "serverless" feature soon, you can follow that here: http://ideas.stormpath.com/ideas/IAM-I-59

这篇关于发布请求时出现401错误Stormpath Express + React + Node + Gulp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆