【发布时间】:2022-08-05 23:02:20
【问题描述】:
设法在 Postman 中获得 JSON 响应,但在控制台中得到一个空响应,没有任何错误。
这是代码:
function getFetch() {
const url = `https://fantasy.premierleague.com/api/bootstrap-static/`;
let requestOptions = {
method: \"GET\",
redirect: \"follow\",
mode: \'no-cors\',
\"Access-Control-Allow-Origin\": \"*\",
\"Access-Control-Allow-Methods\": \"GET\",
\"Access-Control-Allow-Headers\": \"x-requested-with\"
};
fetch(
\"https://fantasy.premierleague.com/api/bootstrap-static/\",
requestOptions
)
.then((response) => response.text())
.then((result) => console.log(result))
.catch((error) => console.log(\"error\", error));
}
P.S:如果我在浏览器中输入 URL,我也会得到 JSON。但不是用 fetch
-
当您明确向 3rd 方 api 发出跨域请求时,为什么要将其设置为 no-cors?
-
这回答了你的问题了吗? Trying to use fetch and pass in mode: no-cors
标签: javascript rest fetch-api