[英]How can I fetch data from yelp api in React JS?
我想从 React JS 中的 Yelp API 获取数据。 我可以使用邮递员获取数据,但无法使用我的代码获取任何数据。 这是代码。
componentDidMount = () => {
const API_BASE_URL =
'https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/business/search?term=restaurant&location=usa';
const BEARER_TOKEN =
'mybearertoken';
fetch(`${API_BASE_URL}`, {
headers: {
Authorization: `${BEARER_TOKEN}`,
Origin: 'localhost:3000',
withCredentials: true,
},
})
.then(res => res.json())
.then(json => {
this.setState({
isLoaded: true,
items: json,
});
});
};
尝试
headers: {
'Content-Type': 'application/json'
}
我 npm 安装并使用了 Axios,它工作正常。
const fetchtems = async () => {
const data = await axios
.get(
`${'https://cors-anywhere.herokuapp.com/'}https://api.yelp.com/v3/businesses/search?location=usa`,
{
headers: {
Authorization: `Bearer ${BEARER_TOKEN}`,
},
params: {
term: 'restaurants',
},
},
)
.then(json => {
setItems({ items: json.data.businesses });
})
.catch(err => {
console.log(err);
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.