Asked 6 years ago
28 Dec 2017
Views 1255
angeo

angeo posted

how to connect api with react-native in mobile App Development ?

How to connect api with react-native , i used jquery ajax to call server side api untill now for my all javascript web page but dont know how to do ajax call with react-native

using login form so it must use POST method api call
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

one can use fetch method to call with request object .


var request = new Request("http://mywebsite.com/login.php", {
	method: 'POST',
	mode: 'cors',
	redirect: 'follow',
	headers: new Headers({
		 Accept  : 'application/json',
		'Content-Type' : 'application/json'
	}),
	body :  JSON.stringify ({
		username : 'username',
		password : 'password'
		})
});

// Now use it!
fetch(request).then((response)=>response.json()).
then((res)=>{
console.log(res);
 });

iPhone-coder

iPhone-coder
answered Nov 30 '-1 00:00

Use axios ,
install axios by npm command


npm install axios



axios.post('mywebsite.com/login.php', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });



Post Answer