【发布时间】:2026-01-20 18:35:01
【问题描述】:
我正在尝试将此 API https://www.petfinder.com/developers/api-docs 与 React Native 一起使用。我遇到的问题是它返回 JSONP 而不是 JSON,所以使用 fetch 在 JSON 错误中给了我一个意外的令牌。我尝试使用像 fetch-jsonP 这样的库,但是因为没有文档(与 jquery 相同),所以在 React Native 中不起作用,有什么想法可以使用 React Native 获取 jsonP 数据吗?
App.js
class Home extends Component {
componentDidMount() {
fetch(
`http://api.petfinder.com/pet.find?format=json&key=${API_KEY}&animal=dog&location=84070&callback=callback`
)
.then(res => res.json())
.then(responseText => console.log(responseText))
.catch(err => console.log(err));
}
render() {
const width = Dimensions.get("window").width;
const height = Dimensions.get("window").height;
return (
<View>
<Header
backgroundColor={darkBlue}
leftComponent={{ icon: "menu", color: "#fff" }}
centerComponent={<HeaderTextComponent />}
rightComponent={{ icon: "home", color: "#fff" }}
/>
</View>
);
}
}
export default Home;
【问题讨论】:
-
你试过this吗?
-
是的,当我使用 fetch 调用运行该代码时,我得到“无效的 JSONP 响应”,所以我抛出了错误
标签: react-native jsonp fetch