这可能是重复的,但我还没有找到专门与我的问题相关的线程。
我正在进行以下 API 调用:
const config = {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,PATCH,OPTIONS"
}
};
const {
data: { ip }
} = await axios.get("https://api.ipify.org?format=json", config);
这会引发错误:
Access to XMLHttpRequest at 'https://api.ipify.org/?format=json' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
当我将我的应用程序部署到 Heroku 时,API 调用按预期工作。但是在我的本地机器上开发时它不起作用。不确定我在这里缺少什么。