React + Express + Keystone.js问题,正在发送OPTIONS请求而不是POST进行登录

时间:2018-10-22 15:25:39

标签: javascript node.js reactjs express cors

我编写了一个应用程序,该应用程序在客户端使用react,在后端使用expresskeystone.js

我的问题是,每当我提交包含email and password的登录表单时,浏览器都会发送OPTIONS请求而不是POST请求。根据我所做的研究,看来我很可能CORS有问题。我尝试了几种方法来解决这个问题。

这是我从react应用发送的请求:

function login(email, password) {
  const requestOptions = {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ email, password })
  };

  return fetch(`${url}authenticate`, requestOptions)
    .then(handleResponse)
    .then(user => {
      // login successful if there's a jwt token in the response
      if (user.token) {
        // store user details and jwt token in local storage to keep user logged in between page refreshes
        localStorage.setItem("user", JSON.stringify(user));
      }
      return user;
    });
}

我的keystone.js文件中有一些代码,我尝试使用cors库来解决该问题,但是没有运气。

var keystone = require("keystone");
var express = require("express");
var bodyParser = require("body-parser");
var cors = require("cors");
var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(cors());
app.options("*", cors());
keystone.set("routes", app);

0 个答案:

没有答案