-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.

6
  • 1
    You need to configure CORS on the server side. Commented Feb 12, 2018 at 8:15
  • what do you mean Commented Feb 14, 2018 at 9:24
  • research about CORS and you will understand it. Commented Feb 14, 2018 at 13:13
  • I am installing CROS on chrome and it is not the problem . The problem is how to send token oauth2 in header in post request Commented Feb 15, 2018 at 6:43
  • The problem is CORS. Preflight options 401 is always CORS: stackoverflow.com/questions/42221602/… Commented Feb 15, 2018 at 8:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.