2017-02-12 10 views
7

Buduję naprawdę łatwą api i reaguje natywna aplikacja. Serwer działa dobrze (testowany z PostMan), ale aplikacja nie wywołuje serwera. Blokuje się, gdy axios musi wysłać żądanie postu (patrz poniżej).Axios (w trybie natywnym w trybie React) nie wywołujący serwera w localhost

jestem zdesperowany :-(Utraty zbyt mazi czasu w nim. Proszę, czy możesz mi pomóc ...

Oto moja strona kodowa zalogować. To wysyłką twórcy działania (praca z Redux) dając e-mail i hasło:

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

e-mail i hasło są weel pobierane i wysyłane do twórcy działanie:

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

trzy console.log() przed Axios pokazują dane w prawidłowy w ay. SIGNIN_URL jest dokładnie taki sam, jakiego używam w listonoszu. ... ale aksios nie dzwoni.

Wystarczy dać wszystkie karty, to jest mój sklep:

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

Nie ma komunikatu o błędzie w debuggera (ale ta podana przez wywołanie Axios („Nie można zalogować się”)

jestem na Windows 10, z:

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

wezwanie zawiedzie nawet kiedy przygotować prostą rozmowę GET i serwer ma oddać prostą wiadomość (testowane z listonoszem i przeglądarki):

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

Ostatni, tryed również zmienić wywołanie dodanie nagłówka jak poniżej, ponieważ API jest kodowana przyjąć 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'); 
     } 
    ); 
    }; 
}; 

ale nawet to nie działa. Mam nadzieję, że ktoś może mi pomóc. Inne podobne problemy nie.

+0

Dodatkowe informacje o: Pracuję z Androidem – Pibo

Odpowiedz

11

Rozwiązanie pochodzi z innego źródła, ale zamieszczam tutaj, aby pomóc innym osobom w szukaniu tego samego problemu. Zasadniczo użyłem Androida AVD (emulator) do zbudowania aplikacji. Ale emulator jest w rzeczywistości inną maszyną i dlatego nie mógł wywołać lokalnego hosta.

Aby rozwiązać probleme, musiałem wysłać wniosek w następujący sposób:

https://10.0.2.2:3000/v1/test 

zamiast:

https://localhost:3000/v1/test 
Powiązane problemy