Angular HttpClient POST 服务器(springboot)获取不到参数问题

Angular HttpClient POST 服务器获取不到参数问题

参数是一串json字符串,而不是一个的参数

改前

 

 改后

 

 

改前:

//调用登录接口
    var api = 'http://127.0.0.1/authentication/form';
    var dd=this.validateForm.value;
    const httpOptions = {
      headers: new HttpHeaders({'content-type': 'application/x-www-form-urlencoded'})
      };
    this.http.post(api,{
      "username":"admin",
      "password":"123456",
      "imageCode":"2313"
    },httpOptions).subscribe((response)=>{
        console.log(response);
    });

改后:

transformRequest(data) {
    var str = '';
    for (var i in data) {
    str += i + '=' + data[i] + '&';
    }
    str.substring(0, str.length - 1);
    return str;
  };


//调用登录接口
    var api = 'http://127.0.0.1/authentication/form';
    var dd=this.validateForm.value;
    const httpOptions = {
      headers: new HttpHeaders({'content-type': 'application/x-www-form-urlencoded'})
      };
    this.http.post(api,this.transformRequest({
      "username":"admin",
      "password":"123456",
      "imageCode":"2313"
    }),httpOptions).subscribe((response)=>{
        console.log(response);
    });

 

 

 

 

Angular HttpClient POST 服务器获取不到参数问题

posted @ 2020-12-23 11:16  三瑞  阅读(343)  评论(0编辑  收藏  举报