【发布时间】:2021-08-04 15:36:28
【问题描述】:
我正在制作一个 React 应用程序,为了在开发中调用 API,我在 package.json 中设置了一个代理:
"proxy":"https://www.metaweather.com/api/location",
我正在向这样的天气应用发出 api 请求:
export const getLocations = async (query) => {
const response = await axios.get(`/search/?query=${query}`)//full address is proxied in package.json
return response.data //WORKS PERFECTLY
}
但是,由于 CORS 错误,我无法向同一域的不同 url 发出 api 请求
export const getWeather = async (id) => {
const response = await axios.get(`44418`)
return response.data //CORS PROBLEM
}
我该如何解决这个问题?我已经搜索了一整天,无法弄清楚为什么一个子域可以工作,而另一个子域不能?
【问题讨论】: