【问题标题】:API fetching in ReactNativeReact Native 中的 API 获取
【发布时间】:2021-12-10 20:25:05
【问题描述】:

我叫 Leo,我是 Reactnative 的新手 我正在尝试登录我的应用程序,但遇到了 fetch API 问题。 我收到 [TypeError:网络请求失败]。我在设备 API 30 上运行 这是我的代码:

const loginHadle = async(email, password)=>{
        await fetch ('http://127.0.0.1:8000/login/',{
            method:'POST',
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({"email": email, "password":password})
        }).then(response=>{
            if(response.status==200)
            {
                response.json().then(data=>{
                    signIn(data.email, data.tokens);
                })
            }
        })
        .then(res => {
            console.log("reponse :", res); 
           }).catch(error => {
            console.error(error);
            return { name: "network error", description: "" };
          });

    }

我已经将此行添加到 AndroidManifest.xml

android:usesCleartextTraffic="true"

这是我在邮递员上收到的

非常感谢!

【问题讨论】:

    标签: android react-native api android-emulator localhost


    【解决方案1】:

    在 Android 9 上,因为“http”和问题通过在 AndroidManifest.xml 中添加 android:usesCleartextTraffic="true" 解决了

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    <application android:usesCleartextTraffic="true"> . . . </application>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2020-05-08
      • 2018-06-12
      • 2021-05-24
      • 2019-07-15
      • 2021-12-23
      相关资源
      最近更新 更多