POST issues


#1

I tired login like this

singIn(){
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded' );
    let options = new RequestOptions({ headers: headers });

    let postParams =  {

      "Email": this.username,
      "Password": this.password,  
    } 
    this.http.post("http:mydomainname/api/user",JSON.stringify(postParams),options)
      .subscribe(data => {
        let serverData =data.json()
        let response = serverData.status;
        console.log(serverData)
      }, error => {
        console.log(error);
      });
  }

when i try to print console.log(serverData) print nothing but when i checked XHR response there showing username and password matched but its directly going to error block and showing this below message

Object { _body: error, status: 0, ok: false, statusText: "", headers: {…}, type: 3, url: null }

how can i fixed that please help me out
Thanks


#2

Hi @flycoders_sourav! :wave:

What does the Network tab in the Developer Tools say about this request? Without the body of the actual error is hard to know what may be happening. Also, is this API returning the appropriate CORS headers?

Best,
Rodrigo


#3

Just ignore the message, it would work only in ionic 4…

Hi, your code looks a little bit odd. I think it’s an older version of angular. Try changing:

var headers = new Headers();
headers.append(‘Content-Type’, ‘application/x-www-form-urlencoded’ );

For

const httpOptions = {
headers: new HttpHeaders({
‘content-type’: ‘application/x-www-form-urlencoded’
})
};

And

JSON.stringify(postParams)

For

postParams

Of course you have to change imports too.

Let me know if it’s working :slight_smile: