Angular HttpClient 跨域请求不成功

Angular版本 4.3.3

请求代码:

this.http.post(apiurl,
    {
      username:this.username,
      password:this.password,
      remember:(this.remember ? 1 : 0)
    })
    .subscribe(
      (val) => {
        console.log("POST call successful value returned in body", 
        val);
      },
      response => {
        console.log("POST call in error", response);
      },
      () => {
        console.log("The POST observable is now completed.");
      });

控制台报错信息:

XMLHttpRequest cannot load http://172.16.0.10:8085/xapi/admin/login. Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.
login.component.ts:129 

POST call in error 

clipboard.png

请求相关信息:

clipboard.png

同样的请求,在localhost中,用jquery的$ajax可以成功请求。

clipboard.png

请教,是哪里出了问题呢?怎么破?

阅读 7k
1 个回答

使用HttpParams解决了……
这……

let rem = (this.remember ? '1' : '0');
    const param = new HttpParams().append('username', this.username).append('password', this.password).append('remember', rem );
    this.http
      .post(this.appservice.apiurl + this.appservice.apilist.login, param)
      // See below - subscribe() is still necessary when using post().
      .subscribe(
      (val) => {
        console.log("POST call successful value returned in body", 
        val);
      },
      response => {
        console.log("POST call in error", response);
      },
      () => {
        console.log("The POST observable is now completed.");
      });
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏