मैं प्रतिक्रिया मूल का उपयोग कर एक एंड्रॉइड ऐप विकसित कर रहा हूं। मैं गैलेक्सी नेक्सस एपीआई 23 पर अपने ऐप का परीक्षण कर रहा हूं जिसे मैक पर चल रहे एंड्रॉइड स्टूडियो में नकल किया जा रहा है।एंड्रॉइड पर प्रतिक्रिया मूल, fetch के साथ AJAX कॉल
मैं उपयोगकर्ताओं को एक दूसरे के साथ जानकारी साझा करने की अनुमति देना चाहता हूं। मैं php और mysql के साथ एक वेबसर्वर चला रहा हूं, और मैं चाहता हूं कि ऐप उपयोगकर्ताओं के बीच डेटा साझा करने के लिए सर्वर से अनुरोध करे।
मैं 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 के होते हैं:
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();
बुलाया मेरी एमुलेटर में मैं निम्न त्रुटि प्राप्त जब मैं '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'
एक वेब ब्राउज़र में दोनों लोड, curl
, और wget
। मैंने 127.0.0.1 के साथ 10.0.2.2 को बदलने का भी प्रयास किया है। मैंने अपने स्थानीय आईपी (12 9.xxx.x.x)
का उपयोग करने का भी प्रयास किया है, इन अनुरोधों को करने के लिए मुझे क्या करने की आवश्यकता है? मैं एम्यूलेटेड एंड्रॉइड ऐप से लोकहोस्ट पर चल रहे अपने विकास सर्वर का उपयोग कैसे करूं?