【发布时间】:2018-12-05 20:21:06
【问题描述】:
我正在使用 Axios 向不同域中的 API 服务器创建 HTTP 请求。
- API 服务器允许来自
http://localhost:3000的跨域请求。 - 我无法控制 API 服务器。
- 我的应用程序通常在
http://localhost:3000中运行并从浏览器发出请求。
到目前为止没有问题。跨域请求工作正常。但是,最近我想为这些 API 调用添加一个单元测试。这个测试环境是jsdom,因为我使用的是Jest。当我从服务器端创建 HTTP 请求时,这会引发一个问题,源设置为 http://localhost,这是服务器不允许的。
请求是使用 Axios 发出的:
axios.post(`${API_DOMAIN}/member/account/login`, {
username,
password,
}, {
headers: {
Origin: 'http://localhost:3000'
}
})
但是,响应仍然说
error: Cross origin http://localhost forbidden
如何将我在jsdom下用axios创建的http请求的“来源”改为http://localhost以外的?我需要它是 http://localhost:3000 以便 API 服务器允许我。
【问题讨论】: