-1

This code did not work:

createRoute(FormRoute) {
    var body = "&name=" + FormRoute.name + "&description="+ FormRoute.description ;
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded' );
    headers.append('Authorization',  'oauth ' + localStorage.getItem("access_token"));
    return new Promise((resolve, reject) => {
        this.http.post('url', body, {headers: headers})
        .toPromise()
        .then((response) =>
        {
            var res = response.json();
            console.log(res);
            resolve(response.json());
        })
        .catch((error) =>
        {
            var err = error.json();
            console.error("err");
            console.error(err["error_description"]);
            let alert = this.alert.create({
              title: 'Error',
              subTitle: err["error_description"],
              buttons: ['OK']
            });
            alert.present();
        });
    });
}

OPTIONS 'http://url' 401 (Unauthorized)

Failed to load 'http://url': Response for preflight has invalid HTTP status code 401.

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
Ahmed
  • 49
  • 2
  • 10

1 Answers1

1

I believe the following line of code is the culprit here.

this.http.post('url', body, {headers: headers})

I don't think you are trying to post to http://url as it doesn't make any sense.

Change it into following.

this.http.post(url, body, {headers: headers})

Define your url somewhere like:

var url = 'http://httpbin.org/'

Hope this helps.

Nabin Paudyal
  • 1,591
  • 8
  • 14
  • i add the url like you did i got error (index):1 Failed to load http://.../api/: Response for preflight has invalid HTTP status code 401. – Ahmed Feb 14 '18 at 09:25