Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data to server and fetched response using react native application?

Tags:

react-native

I am trying to learn react native application by making a simple login page using api call. Where I will send user name and password to api and it will give me response. But I can't do it. Well I am sharing my code here.....

var myRequest = new Request('<my API >', {method: 'POST', body: '{"username":this.state.uName , "password":this.state.pwd}'});
    fetch(myRequest).then(function(response) {
        alert('Res'+response);
    }).then(function(response) {
        alert('Blank'+response);
    })
    .catch(function(error) {
        alert('Error'+error);
    });

I think the way of passing my user name and password to server is wrong. Please give me some idea then I can understand what is wrong here.

like image 978
R. Dey Avatar asked Apr 17 '17 07:04

R. Dey


1 Answers

var data = {
   "username": this.state.username,
   "password": this.state.password
}

fetch("https://....", {
   method: "POST",
   headers: headers,
   body:  JSON.stringify(data)
})
.then(function(response){ 
 return response.json();   
})
.then(function(data){ 
console.log(data)
});

I hope this helps.

like image 88
Ujjwal Nepal Avatar answered Oct 27 '22 00:10

Ujjwal Nepal