-1

我正在尝试通过以下方式提交表单:

saveBuildcompany(): void {
    // @ts-ignore

  
    // @ts-ignore
    console.log(this.group?.value);
    
   let data2=this.group.value;
    let serializedForm = JSON.stringify(data2);

   console.log(data2);
   // data2.sociallinks.stringify;
    this.buildcompanyService.create(serializedForm)
      .subscribe({
        next: (res) => {
          console.log(res);
          this.submitted = true;
        },
        error: (e) => console.error(e)
      });
  }

服务如下:

create(data: any): Observable<any> {
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    headers.append('Accept', 'application/json');
    return this.http.post(baseUrl+"/add", data, {headers: headers});
  }

毕竟我得到了标题中的异常。我做错了什么?

4

3 回答 3

0

如果您要发送表单数据,请将“Content-Type”更改为application/x-www-form-urlencoded“Accept” 。application/json

create(data: any): Observable<any> {
    let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'});
    return this.http.post(baseUrl+"/add", data, {headers: headers});
  }

或者

create(data: any): Observable<any> {
    let headers = new HttpHeaders();
    headers.set('content-type', 'application/x-www-form-urlencoded')'
...
    return this.http.post(baseUrl+"/add", data, {headers: headers});
  }
于 2022-01-19T19:34:12.487 回答
0

您可以将“Content-Type”更改为application/x-www-form-urlencoded“Accept” application/json。并尝试以下是代码片段。

create(data: any): Observable<any> {
    let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'application/json'});
    return this.http.post(baseUrl+"/add", data, {headers: headers});
  }
于 2022-01-19T19:51:17.503 回答
0

只是总结一下我上面的所有悲伤。实际的创建函数是这样的。这是发送正确的标头信息:

create(data: any): Observable<any> {
    let headers = new HttpHeaders({'Content-Type': 'application/json', 'Accept': 'application/json'});
    return this.http.post(baseUrl+"/add", data, {headers: headers});
  }
于 2022-01-20T05:17:19.653 回答