下午好。这是react应用程序。我想使用api,但我遇到了错误,你能帮我吗?
null
const API_URL = 'https://calorieninjas.com/v1/nutrition?query='
const headers = {
headers: {
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
'X-Api-Key': 'SUDsfgqcBvJi0xM/au83yA==T8nulwvUA6YoMWbN'
}
}
const getNutrition = () => {
axios(`${API_URL}${products}`, headers)
.then(({data}) => setNutrition(data))
}
null
我会遇到这样的错误:
CORS策略已阻止从起源“http://localhost:3000”访问位于“https://calorieninjas.com/v1/nutrition?query=tomato”的XMLHttpRequest:请求的资源上不存在“Access-Control-Allow-Origin”标头。
试试这个chrome扩展:https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf
这只是为了测试目的。