Angular 2 XMLHttpRequest

有些话、适合烂在心里 提交于 2019-12-25 08:57:41

问题


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.


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/45925386/angular-2-xmlhttprequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!