axios 发布请求适用于 React Native ios 但不适用于 android

cki*_*m16 5 android react-native axios react-native-android

我知道关于这个问题有很多答案,但我似乎找不到适合我的答案。我正在使用 axios 向我的服务器发送一个 post 请求,但它在 android 中不起作用,尽管它在 ios 中起作用。我目前正在使用服务器 ip 地址(不是本地主机),并且我也在请求时发送标头,但它仍然没有通过 android 的网络请求。

import axios from 'axios';

const SERVER_URL = 'http://serverip:3000';

export function signin({ username, password }) {
  return function(dispatch) {
    axios.post(`${SERVER_URL}/user/authenticate`, { username, password }, { headers: { 'Content-Type': 'application/json' } })
    .then((response) => {
      console.log('login response', response);
      dispatch({
        type: USER_AUTH,
      });
      AsyncStorage.setItem('token', response.data.token || '');
    })
    .catch((response) => console.log('user sign in err', response));
  };
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 有没有人像我一样有类似的问题并且知道如何进行这项工作?

谢谢,

小智 0

将标题设置为

  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  },
Run Code Online (Sandbox Code Playgroud)