Pedro de Albuquerque
Pedro de Albuquerque

Reputation: 133

Angular 2 XMLHttpRequest

I'm trying to make a POST request to an API and I'm receiving this error:

XMLHttpRequest cannot load http://*. Response for preflight has invalid HTTP status code 400 However, when I try to make a GET request, it works.

My get method:

getMethod() {
    return this.http.get<PerfilModel[]>(globals.BASE_URL + 'perfis');
}

My post method:

updateMethod(changePass: ChangePassModel) {
    let h = new Headers();
    h.append('Content-Type', 'application/json');

    return this.http2.post(globals.BASE_URL + 
                           'users/passwords/redefinitions/' + 
                           changePass.key, JSON.stringify(changePass),      
                           { headers: h }
                           ).map(res => res.json);
}

observation: the cors toggle plugin is activated.

Upvotes: 0

Views: 1438

Answers (3)

Pedro de Albuquerque
Pedro de Albuquerque

Reputation: 133

The problem was on the browser same origin policy, so I disabled the security with the flag: --args --disable-web-security --user-data-dir="" and it worked.

Upvotes: 0

Robert
Robert

Reputation: 3483

globals.BASE_URL + 'users/passwords/redefinitions/' + changePass.key + '/'

if the cors toggle plugin is activated in your backend,your url missing "/" at last,can you check that

Upvotes: 1

Shanmugapriya D
Shanmugapriya D

Reputation: 306

The HTTP 400 Bad Request response status code indicates that the server could not understand the request due to invalid syntax. The client should not repeat this request without modification.

May be server side they are allowing the post method.

Upvotes: 1

Related Questions