我曾经使用以下代码来检索JSON文件,该文件在我的React Naive源代码中包含个人数据:
async componentDidMount() {
try {
const response = await fetch('mydomain.org/personaldata.json');
const responseJson = await response.json();
this.setState({
isLoading: false,
dataSource: responseJson,
});
}
catch (error) {
console.error(error);
}
}然后我决定使用RapidAPI使它更安全,但我不知道如何使用它,它给我的白色页面没有数据:这是我修改后的代码:
async componentDidMount() {
try {
const response = await fetch('coin-flip1.p.rapidapi.com/headstails', {
"method": "GET",
"headers": {
"x-rapidapi-host": "mydomain.org",
"x-rapidapi-key": 'lkweruytv43578tv3urhgciuyv2b738465873465c87xnb746'
}
});
const responseJson = await response.json();
this.setState({
isLoading: false,
dataSource: responseJson,
});
}
catch (error) {
console.error(error);
}
}这是他们文档中的RapidAPI代码:
const fetchData = () => {
startFlip()
setLoading(true);
fetch('https://coin-flip1.p.rapidapi.com/headstails', {
"method": "GET",
"headers": {
"x-rapidapi-host": "coin-flip1.p.rapidapi.org",
"x-rapidapi-key": 'apikey'
}
})
.then((response) => response.json())
.then((json) => setData(json.outcome))
.catch(() => Alert.alert('Something went wrong..', 'There was an error fetching coin flip.'))
.finally(() => {
setLoading(false)
resetFlip()
});
};发布于 2021-08-20 10:15:08
我假设你在RapidAPI上使用Coin Flip API。如果我使用axios,它对我来说工作得很好。
尝试使用此代码片段
var axios = require("axios").default;
var options = {
method: 'GET',
url: 'https://coin-flip1.p.rapidapi.com/headstails',
headers: {
'x-rapidapi-key': '12345',
'x-rapidapi-host': 'coin-flip1.p.rapidapi.com'
}
};
axios.request(options).then(function (response) {
console.log(response.data.outcome);
}).catch(function (error) {
console.error(error);
});https://stackoverflow.com/questions/64737301
复制相似问题