Erro when try to link to php


#1

hi guys
i try to build a page to approve a login my code here :
constructor(public nav: NavController, private http: Http ) {
this.nav = nav;
this.http = http;
this.user = {};
this.user.username = ‘’;
this.user.password = ‘’;
this.user.response = ‘’;

         }
   verifylogin(username, password){
 
    let link = 'xxx/Login.php';
    let user = JSON.stringify({username: this.user.username, password: this.user.password});
     
    this.http.post(link, user)
    .subscribe(user => {
    
      
        this.user.response =  user._body;
        this.flag = true;
    },
        error => {
            console.log("error");
        }); 

        if (this.flag == true)
     localStorage.setItem('user',JSON.stringify(user));

}

`
but i got an erro in this statement :

this.user.response = user._body;

i use ionic 2 type script and i need to send from app user name and password to server side to authenticate and return the response so any adive if i am doing something wrong and even why this error is occur ?

Thank you in advance


#2

Is your server expecting JSON from the request or normal FORM parameters, if it’s the latter case, you’ll probably need

let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post(link, 'username=xxx&password=xxx', {headers: headers})

#3

thank you i solved this by chaging this from post to get :slight_smile:


#4

That’s not a solution. It’s extremely insecure and breaks the fundamental principles of REST. Logging a user in absolutely needs to be a POST operation: it is not idempotent, it modifies state, and you are going to end up with cleartext user credentials in browser history and server logs. Very bad.