我目前正在尝试使用 Atlassian Jira rest API。为了不出现 CORS 错误,我采用了不从浏览器发送请求而是通过我的快速服务器代理它的推荐路线。
现在,当我这样做时,我在应用程序中收到的只是一个未决的承诺。我假设我有一次没有正确解决它,但我不知道在哪里。
API Handler 向代理发送请求:
const baseURL = `${apiConfig}/jiraproxy`;
export const testConnection = integration => {
return fetch(`${baseURL}/get`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(integration)
})
.then(handleResponse)
.catch(handleError);
};
Express 服务器上的 Jira 代理端点
const baseURL = `rest/api/3/dashboard`;
router.post("/get", (req, res) => {
fetch(req.body.link + baseURL, {
method: "GET",
headers: { Accept: "application/json" },
auth: {
username: req.body.credentials.username,
password: req.body.credentials.token
}
})
.then(handleResponse)
.catch(handleError);
});
处理响应和处理错误方法:
async function handleResponse(response) {
if (response.ok) {
return response.json();
}
if (response.status === 400) {
const error = await response.text();
throw new Error(error);
}
throw new Error("Network response was not ok.");
}
function handleError(error) {
// eslint-disable-next-line no-console
console.error(`API call failed. ${error}`);
throw error;
}
目标: 向代理发送发送请求的请求,并将代理的响应作为初始“testConction”方法的返回返回。
错误: 没有抛出错误,但在浏览器中收到的响应是待处理的承诺。