2017-02-12 22 views
7

我正在构建一个非常简单的api和react-native应用程序。服务器运行良好(使用PostMan进行测试),但应用程序不会调用服务器。它会阻止axios必须发送发布请求(见下文)。Axios(在React-native中)不在本地主机中调用服务器

,我渴望在:-(松动它过分指望时间。请,如果你能帮助我...

这里是我的代码登录页面。它派遣行动的创建者(与终极版工作)给予电子邮件地址和密码:

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

电子邮件和密码WEEL检索和发送到行动的创建者:

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

三米的console.log()爱可信显示正确的W上的数据之前,唉。 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; 
}; 

有在调试器没有错误信息(但一个由爱可信调用(“无法登录”给出)

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

最后,我tryed还修改呼叫添加标题为以下,因为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'); 
     } 
    ); 
    }; 
}; 

但即使这样没有工作。希望有人能帮助我。其他类似的问题没有。

+0

额外的相关信息:我与Android合作 – Pibo

回答

11

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

为了解决这个probleme,我送过下列方式请求:

https://10.0.2.2:3000/v1/test 

代替:

https://localhost:3000/v1/test 
相关问题