0

I am beginner for Ionic application development and i am trying to post data to server using below code but my code does not hit server url and i am getting failure response please help me what should for complete my requirement

home.ts:

doLogin() {

    //Headers parameters
    var headers = new Headers();
    headers.append('Content-Type', 'application/json');
    let options = new RequestOptions({ headers: headers });

    //Parameters
    let postParams = {
      empId: 'em2',
      password: 'password123',
      deviceId: '957334kjhsd'
    }

    this.http
        .post('http://2c631a4d.ngrok.io/employeeLogin', postParams, options)
        .map(res => res.json())
        .subscribe(
            data => {
              alert("success")
            },
            err => {
              alert("failure")
            }
        );
  }
AbhiRam
  • 2,033
  • 7
  • 41
  • 94

1 Answers1

0

You cant post to server-side due to cross-domain issue, you must allow same origin request in your server, allow localhost request in your server. If you are using asp.net server, add some line of code in your webconfig and your router class.

Ionic http post is a few buggy. You must pass your data as querystring like this:

Let params = "id="+MyId"+"&title="+MyTitle;
this.http.post(url, params, options) .map(res => res.json())
mohsen solhnia
  • 366
  • 3
  • 15