【发布时间】:2022-01-28 06:52:30
【问题描述】:
我知道这是一个老问题,但我不知道如何解决这个问题。我从本地机器调用 API 请求到云服务器。我昨天和之前都试过了,效果很好。但是今天当我尝试它时,它返回 CORS 错误。服务器端使用 nodejs 和 express js 作为其框架。我已经用npm install cors 安装了 cors 并在 index.js 中使用它。
这是来自 mozilla 的错误堆栈:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://appbk.myweb.com/api/v1/user/. (Reason: CORS request did not succeed).
这是我的 index.js
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors')
app.use(cors());
这是我对 axios 的要求
function createHeader() {
var timestamp = createTimestamp();
var signature = createSignature(timestamp);
const headers = {
"id" : id,
"time" : timestamp,
"token" : signature,
"Content-Type" : "application/json",
};
return headers;
}
export async function getUserList() {
var response = await Axios.get(baseUrl + "user/", {headers: createHeader()})
return response.data.data;
}
很困惑,因为昨天它工作正常,我根本没有更改任何代码。谢谢。
【问题讨论】:
-
您引用的错误消息提到了
https://appbk.myweb.com/api/v1/banner/all/,但对Axios.get的调用使用以user/结尾的URL。你没有告诉我们一切...... -
@jub0bs 哎呀,我贴错了网址,谢谢提醒
标签: node.js reactjs axios cors