角7不发送请求头

时间:2019-03-11 05:36:56

标签: javascript angular http http-headers angular7

我正在尝试为以下发布请求发送内容类型标头,以允许json请求,但是使用 OPTIONS 请求方法,它向我抛出了错误无效的CORS请求 。它甚至不发送 POST 方法。

在这里,我无法使用已贬值的RequestOptions

PS:当我与邮递员发送请求时,此方法工作正常。并且,后端代码已通过CORS请求处理。

从后端Java代码中,这是我得到的错误

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported

我在哪里想念?

 postSubmit(){

        let data = { "name":"John", "age":30 };

         const httpHeaders = new HttpHeaders ({
            'Content-Type': 'application/json'
          });

            return this.http.post<any>(apiURL, data, {headers : httpHeaders})
            .pipe(catchError(error => this.handleError(error)))
          }
        }

1 个答案:

答案 0 :(得分:1)

要使用新的HttpHeaders类定义内容类型,

  1. 只需导入import { HttpHeaders } from '@angular/common/http';
  2. 创建将传递给每个HttpClient保存方法的httpOptions对象

    import { HttpHeaders } from '@angular/common/http'; const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'my-auth-token' }) };

  3. 调用API this.http.post<Datatype>(API url, Bady Parameter, httpOptions)

相关问题