我目前正試圖在我的手機上運行我的react-native/rails應用程式以進行測驗。我可以很好地運行我的登錄和登錄螢屏,因為在輸入用戶資訊之前它們不會與我的服務器通信。運行我的服務器時,我使用: $ rails s --binding=0.0.0.0
除了知道我的服務器沒有被通信之外,我沒有收到任何錯誤。這一切在我的 Android Studio 模擬器上也能正常作業。
// one of my fetch GET requests
export function requestCurrentUser(username, auth_token) {
return function action(dispatch) {
const request = fetch(`'http://10.0.2.2:3000'/users/${username}`, {
method: 'GET',
headers: {
"Authorization": auth_token
}
});
return request.then(
response => response.json(),
err => console.log("user error"),
)
.then(
json => dispatch(receiveCurrentUser({id: json.id, email: json.email, username: json.username})),
err => console.log("user json error"),
);
}
}
我嘗試將我的電話 IP 設定更改為 10.0.2.2 網關,并在我的獲取請求中使用我的電話 IP。我覺得我在概念上遺漏了一些東西。提前致謝。
uj5u.com熱心網友回復:
在獲取請求中,您需要使用運行 rails 服務器的機器的 IP,可能是您的筆記本,并使用相同的網路連接您的應用程式和您的 rails 后端 api。為了測驗,您可以嘗試在手機瀏覽器中直接訪問您的api訪問http://IP_FROM_RAILS_MACHINE:3000
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/457203.html
