繁体   English   中英

我怎样才能用 React Native 在 axios 中得到 json?

[英]How can i get json in axios with react native?

我正在尝试在 axios 中获取 json

但是如果我使用我的代码,就会出现这个错误和警告

我怎样才能得到response.json?

在此处输入图像描述

响应.json 不是 function

这是我的代码

      //    url="https://yts.lt/api/v2/list_movies.json?sort_by=like_count&order_by=desc&limit=5"
            
            url is props

     useEffect(() => {
        axios
          .get(url)
          .then((response) => response.json())
          .then((json) => {
            console.log('json', json);
            setData(json.data.movies);
          })
          .catch((error) => {
            console.log(error);
          });
      }, []);

来自 axios 的响应 object 将其数据存储在response.data中。

useEffect(() => {
        axios
          .get(url)
          .then((response) => {
            const json = response.data;
            console.log('json', json);
            setData(json.data.movies);
          })
          .catch((error) => {
            console.log(error);
          });
      }, []);

用这个:

useEffect(() => {
        axios
          .get(url)
          .then((response) => response.data)
          .then((json) => {
            console.log('json', json);
            setData(json.data.movies);
          })
          .catch((error) => {
            console.log(error);
          });
      }, []);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM