Axios(在React本地)不在本地主机调用服务器

我正在构build一个非常简单的API和反应原生应用程序。 服务器工作正常(用PostMantesting),但应用程序不调用服务器。 它会阻止axios发送邮件请求(见下文)。

我绝望了:-(放松的时间,请帮忙,如果你能帮我…

这是我的代码LogIn页面。 它派遣行动的创造者(使用REDX)给予电子邮件和密码:

... 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()); } } }, ... 

电子邮件和密码被取回并发送给动作创build者:

 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; }; 

debugging器中没有错误消息(但是由axios调用给出的('无法login'))

我在Windows 10,与:

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

即使我准备了一个简单的GET调用,服务器应该返回一个简单的消息(用邮递员和浏览器testing),调用失败:

 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'); } ); }; }; 

但即使这样也行不通。 希望有人能帮助我。 其他类似的问题没有。

解决scheme来自不同的来源,但我在这里发布它来帮助其他人寻找同样的问题。 基本上我使用Android的AVD(模拟器)来构build应用程序。 但模拟器实际上是另一台机器,这就是为什么它不能调用本地主机。

为了解决这个问题,我必须通过以下方式发送请求:

 https://10.0.2.2:3000/v1/test 

代替:

 https://localhost:3000/v1/test