Reputation: 1
Im trying to use fetch API to communicate with my server on the localhost, fetch request throws the following error:
[Unhandled promise rejection: TypeError: Network request failed]
- node_modules\whatwg-fetch\dist\fetch.umd.js:473:29 in xhr.onerror
- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:574:29 in setReadyState
- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse
- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0
* [native code]:null in callFunctionReturnFlushedQueue
I have read that ios does only support https requests even though I was able to do this before, the only difference is that I was using xampp/mysql and now im using nodejs with mongoose.
I simply use the following on the front end
fetch("http://localhost:3000/user/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
console.log("Success:", data);
console.log("Success");
})
.catch(error => {
console.log(error);
});
The server is working correctly, I can normally post data and login successfully using postman. (Using JSON) I have also tried inputting just a random google url (https://google.com) and it returned a readable error which is logical which supported the https concern.
Edit: It appears this has nothing to do with iphones nor ssl, I have installed an android emulator (Pixel XL, R Software), the emulator is not reaching my localhost, I have also tried using my pc's local ip.
I have no idea why its not communicating with the server, is it some expo configuration?
Upvotes: 0
Views: 3053
Reputation: 11
Try using your ip address as the baseUrl: similar to "https://192.XXX.X.XX:3000"
To get it from the terminal run ifconfig
and it will be there.
Upvotes: 1
Reputation: 1495
you can setup a self signed certificate on your local web server see: https://shellcreeper.com/how-to-create-valid-ssl-in-localhost-for-xampp/ or you can google other terms similar to "Setup self signed certificate on localhost".
Upvotes: 0