天天看點

Android會用到ajax嗎,React Native在Android上,ajax調用與提取失敗

我正在開發一個使用React Native的Android應用程式。我正在Galaxy Nexus API 23上測試我的應用程式,該應用程式在Android Studio中模拟,并在Mac上運作。React Native在Android上,ajax調用與提取失敗

我希望允許使用者互相共享資訊。我使用php和mysql運作一個web伺服器,并且我希望應用程式向伺服器送出請求以在使用者之間共享資料。

我想使用Fetch API向我的開發伺服器發出POST請求。我的JS代碼如下:

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

});

}

};

通過AjaxEngine.test();

叫我在模拟器我收到以下錯誤:

test.json包括:

{

"message":"hello world"

}

當我用'http://10.0.2.2/test.json'替換時,請求成功'http://facebook.github.io/react-native/movies.json',是以函數本身就沒問題。

'http://localhost/test.json'和'http://127.0.0.1/test.json'都負載在Web浏覽器,curl和wget。我也嘗試用127.0.0.1替換10.0.2.2。我也嘗試使用我的本地IP(129.xxx.x.x)

我需要做些什麼才能使這些請求有效?我如何從模拟的android應用程式通路在本地主機上運作的開發伺服器?