React Nativeを使用してAndroidアプリを開発しています。私はAndroidスタジオでエミュレートされているGalaxy Nexus API 23で自分のアプリをテストしています。AndroidでNativeに反応し、フェッチでAJAX呼び出しが失敗する
私は、ユーザー同士が情報を共有できるようにしたいと考えています。私はphpとmysqlを使ってWebサーバーを稼働しています。ユーザーにデータを共有するようにサーバーに要求するようにアプリに要求します。
Fetch APIを使用して開発サーバーにPOSTリクエストを作成しようとしています。私のjsのコードは以下の通りです:
{
"message":"hello world"
}
:
ExceptionsManager.js:70 TypeError: Network request failed at XMLHttpRequest.xhr.onerror (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:23711:8) at XMLHttpRequest.dispatchEvent (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:9740:15) at XMLHttpRequest.setReadyState (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:25157:6) at XMLHttpRequest.__didCompleteResponse (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:25015:6) at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:25090:52 at RCTDeviceEventEmitter.emit (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:8977:23) at MessageQueue.__callFunction (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:7065:23) at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:6969:8 at guard (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:6917:1) at MessageQueue.callFunctionReturnFlushedQueue (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:6968:1)
test.jsonは、から構成されています。私のエミュレータではAjaxEngine.test();
によって呼び出さ
var AjaxEngine = {
test: function() {
return fetch('http://10.0.2.2/test.json')
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
return responseJson.movies;
})
.catch((error) => {
console.error(error);
});
}
};
私は次のエラーを受け取ります'http://10.0.2.2/test.json'
を次のように置き換えると、リクエストは成功します。 'http://facebook.github.io/react-native/movies.json'
なので、関数自体はokです。
'http://localhost/test.json'
と'http://127.0.0.1/test.json'
Webブラウザの両方の負荷、curl
、およびwget
。また、10.0.2.2を127.0.0.1に置き換えようとしました。私も私のローカルIP(129.xxx.x.x)を使って試しました
これらの要求を動作させるためには何が必要ですか?エミュレートされたアンドロイドアプリからlocalhost上で実行している開発サーバーにアクセスするにはどうすればよいですか?