【问题标题】:React Native fetch() error in version 0.44.0版本 0.44.0 中的 React Native fetch() 错误
【发布时间】:2023-03-29 05:01:01
【问题描述】:

这是一个在 android 模拟器中使用 0.44.0 版本的反应原生测试代码。我可以从许多网站获取数据,但有些网站无法获取。下面提供了一个这样的网站。

当我将 react 原生版本升级到 0.55.0 时,我能够获取数据。但是,在我的主项目中,我一直在使用 0.44.0 版本并且无法升级它。所以,我需要一个不升级反应原生版本的解决方案。我浏览了许多 stackoverflow 页面并找到了 ios、localhost 等的解决方案。但是,找不到任何解决此问题的方法。

import React, { Component } from 'react';

import { Text,View } from 'react-native';


export default class App extends Component {

  checkaccess = () => {
    url = 'https://example.com/wc-api/v3/'
    fetch(url, {
      method: 'GET',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
      }
    })
      .then((response) => {
        console.log('got')
       // console.log(response.json())
        return response.json()
      })
      .catch((error) => {
        console.log('error',error)
      })
      .done()
}


  render() {
    return (
      <View>
        {this.checkaccess()}
        <Text>test</Text>
      </View>
    );
  }
}

控制台日志是:

error TypeError: Network request failed
    at XMLHttpRequest.xhr.onerror (index.bundle:12366)
    at XMLHttpRequest.dispatchEvent (index.bundle:13277)
    at XMLHttpRequest.setReadyState (index.bundle:15382)
    at XMLHttpRequest.__didCompleteResponse (index.bundle:15238)
    at index.bundle:15333
    at RCTDeviceEventEmitter.emit (index.bundle:3851)
    at MessageQueue.__callFunction (index.bundle:2090)
    at index.bundle:1948
    at MessageQueue.__guard (index.bundle:2062)
    at MessageQueue.callFunctionReturnFlushedQueue (index.bundle:1947)

【问题讨论】:

    标签: android reactjs react-native fetch emulation


    【解决方案1】:

    你应该试试这个

    fetch(url, {
        credentials: 'include',
        ...otherOptions,
    });
    

    对于其他选项或理解检查thisgithub上的讨论react-native问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 2018-10-12
      • 2021-06-15
      相关资源
      最近更新 更多