首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法连接到API资源

无法连接到API资源
EN

Stack Overflow用户
提问于 2021-03-26 03:13:41
回答 1查看 43关注 0票数 1

我创建了一个向我的api发出axios post请求的React前端。来自前端的代码在用户成功登录后调用。用户成功登录后,将通过cookie发送一个身份验证令牌,该cookie用于验证用户是否为管理员,是否能够访问api资源/api/v1/order。当我在本地环境中时,这是有效的,但在我将其上传到云之后,我得到了这个错误:

代码语言:javascript
复制
<MY_API>/api/v1/orders:1 Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Adminpage.js:31 Error: Request failed with status code 500
    at e.exports (createError.js:16)
    at e.exports (settle.js:17)
    at XMLHttpRequest.p.onreadystatechange (xhr.js:62)

REACT前端

Adminpage.js

代码语言:javascript
复制
const loadOrders = async () => {
    try {
      const res = await axios.get(
        "<MY_API>/api/v1/orders",
        {
          withCredentials: true,
        }
      );
      console.log(res.data.data.orders);
      setOrderData([...res.data.data.orders]);
    } catch (err) {
      console.log(err);
    }
  };

NODEJS后端

orderRoutes.js

代码语言:javascript
复制
router
  .route('/')
  .get(
    // Only admins can access this route
    authController.requireSignin,
    authController.isAdmin,
    orderController.getAllOrders
  )
  .post(orderController.addOrder);

authController.js

代码语言:javascript
复制
exports.requireSignin = expressJwt({
  secret: process.env.JWT_SECRET,
  algorithms: ['HS256'], // added later
  requestProperty: 'auth', // Decodes the token and assigns to auth object in request object
  getToken: function (req) {
    if (req.cookies.Authorization) {
      return req.cookies.Authorization;
    }
    return null;
  },
});

exports.isAdmin = (req, res, next) => {
  // If users role specified in the req.auth object is not admin, an error is passed to the global error handler otherwise, is able to go to the next function
  if (req.auth.role !== "admin") {
    return next(
      new ApiError(undefined, 403, "User is not authorized for access!")
    );
  }
  next();
};

我的前端React代码托管在AWS上,我的后端Nodejs托管在Azure上。

EN

回答 1

Stack Overflow用户

发布于 2021-03-26 03:54:27

听起来像是CORS问题。您是否在托管应用程序接口的应用程序服务中启用了CORS?https://github.com/uglide/azure-content/blob/master/articles/app-service-api/app-service-api-cors-consume-javascript.md

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66806158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档