最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

Axios(在React

IT培训 admin 3浏览 0评论

Axios(在React

我正在构建一个非常简单的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('', head)
    .then(
      (response) => {
        console.log(response);
      }
    )
    .catch(
      (error) => {
        console.log('error');
      }
    );
  };
};

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

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

回答如下:

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

Axios(在React

我正在构建一个非常简单的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('', head)
    .then(
      (response) => {
        console.log(response);
      }
    )
    .catch(
      (error) => {
        console.log('error');
      }
    );
  };
};

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

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

回答如下:

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

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论