Axios(在React-native中未在本地主机中调用服务器)

问题描述 投票:13回答:7

我正在构建一个非常简单的api和react-native应用程序。服务器运行良好(已通过PostMan测试),但应用程序未调用服务器。当axios必须发送发布请求时,它会阻止(请参见下文)。

我很绝望:-(忙得不可开交。请,如果您能帮助我...

这是我的代码“登录”页面。它分派动作创建者(与redux一起使用)并提供电子邮件和密码:

...
const LogIn = React.createClass({
  submitLogin() {
    // log in the server
    if (this.props.email !== '' && this.props.psw !== '') {
      if (this.props.valid === true) {
        this.props.dispatch(logIn(this.props.email, this.props.psw));
      } else {
        this.props.dispatch(errorTyping());
      }
    }
  },
...

电子邮件和密码被检索并发送给动作创建者:

import axios from 'axios';
import { SIGNIN_URL, SIGNUP_URL } from '../api';
// import { addAlert } from './alerts';
exports.logIn = (email, password) => {
  return function (dispatch) {    
    console.log(email);
    console.log(password);
    console.log(SIGNIN_URL);
    return axios.post(SIGNIN_URL, { email, password })
    .then(
      (response) => {
        console.log(response);
        const { token, userId } = response.data;
        dispatch(authUser(userId));
      }
    )
    .catch(
      (error) => {
        console.log('Could not log in');
      }
    );
  };
};
const authUser = (userId) => {
 return {
 type: 'AUTH_USER',
 userId
 };
};
...

axios之前的三个console.log()以正确的方式显示数据。 SIGNIN_URL与邮递员中使用的完全相同。 ...但是axios不会打电话。

只给所有卡片,这是我的商店:

import thunk from 'redux-thunk';
import { createStore, compose, applyMiddleware } from 'redux';
import { AsyncStorage } from 'react-native';
import { persistStore, autoRehydrate } from 'redux-persist';
import reducer from '../reducer';
const defaultState = {};
exports.configureStore = (initialState = defaultState) => {
 const store = createStore(reducer, initialState, compose(
 applyMiddleware(thunk),
 autoRehydrate()
 ));
 persistStore(store, { storage: AsyncStorage });
 return store;
};

调试器中没有错误消息(但axios调用给出了错误消息('无法登录')

我在Windows 10上,使用:

"axios": "^0.15.3",
"react": "15.4.2",
"react-native": "0.38.0",
"redux": "^3.6.0"

即使我准备了一个简单的GET调用,并且服务器应该返回一条简单的消息(通过邮递员和浏览器测试,该调用也失败了:] >>

exports.test = () => {
  return function () {
    return axios.get('https://localhost:3000/v1/test')
    .then(
      (response) => {
        console.log(response);
      }
    )
    .catch(
      (error) => {
        console.log('error');
      }
    );
  };
};

最后,我也尝试修改添加如下标题的调用,因为api被编码为接受json:

const head = {
  headers: { 'Content-Type': 'application/json' }
};

exports.test = () => {
  return function () {
    return axios.get('https://api.github.com/users/massimopibiri', head)
    .then(
      (response) => {
        console.log(response);
      }
    )
    .catch(
      (error) => {
        console.log('error');
      }
    );
  };
};

但是即使这样也不起作用。希望有人可以帮助我。其他类似问题没有。

我正在构建一个非常简单的api和react-native应用程序。服务器运行良好(已通过PostMan测试),但应用程序未调用服务器。当axios必须发送发布请求时,它会阻止...

node.js react-native axios
7个回答
24
投票

解决方案来自不同的来源,但我将其发布在此处以帮助其他人寻找相同的问题。基本上,我使用Android AVD(仿真器)来构建应用程序。但是仿真器实际上是另一台机器,这就是为什么它不能调用本地主机的原因。


7
投票

如果您使用的是Mac,则此解决方案对我有用。我正在将React Native与Android Simulator ADV一起使用。 Nexus Api 27


3
投票

您的仿真器是一个单独的设备,与网络浏览器,邮递员或服务器不在同一IP(本地主机或127.0.0.1)上运行。


1
投票

我发现axios.get无法解决本机问题的另一种解决方案:


0
投票

另一种解决方案是使用admin cmd中的以下命令在localhost计算机上创建托管网络:


0
投票

[尝试关闭对我有用的防火墙


0
投票
  1. localhost
© www.soinside.com 2019 - 2024. All rights reserved.