【问题标题】:React Native - Can't get data from asp.net web apiReact Native - 无法从 asp.net web api 获取数据
【发布时间】:2019-07-25 00:06:12
【问题描述】:

我正在使用带有 Expo 和 ASP.Net Web API 的 React Native 构建一个应用程序。

我正在使用两台计算机:一台我在其上发布了 Web API,并将其用作具有 IP 192.168.1.9 的服务器,

另一个我用于开发的IP 192.168.1.6

问题是,当我 ping 服务器计算机时,我得到了回复,当我使用邮递员时,我得到了我请求的数据,

但是当我在我的 Android 手机上使用 Expo 运行应用程序时,请求会进入 catch 并返回错误。 代码如下:

var url = 'http://192.168.1.9/trainlast/api/Login';
    fetch(url,
        {
            method: 'GET',
        })
        .then(response => { response.json(); })
        .then(users => {
            this.setState({ allUsers: users });
        })
        .catch(error => console.error('Error getting users :: ', error));

我已经尝试了我能想到的一切,但没有用。

谁能告诉我问题出在哪里?谢谢。

【问题讨论】:

    标签: react-native asp.net-web-api expo


    【解决方案1】:

    你可以配置AcceptContent-Type的headers。

    并获得对象的正确值。

    var url = 'http://192.168.1.9/trainlast/api/Login';
        fetch(url,
            {
                method: 'GET',
                headers: {
                  Accept: 'application/json',
                  'Content-Type': 'application/json',
                  }
            })
            .then((response) => response.json())
            .then(res => {
                this.setState({ allUsers: res.Username });
            })
            .catch(error => console.error('Error getting users :: ', error));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-03
      • 2015-12-24
      • 2018-11-07
      • 2021-09-26
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      相关资源
      最近更新 更多