【发布时间】:2018-03-22 00:44:04
【问题描述】:
我正在尝试使用 POST 请求从服务中获取数据。但我无法更改标题(TS 不会编译)或内容类型。我在控制台中收到此错误:
status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"不支持内容类型'text/plain'"
下面是我的组件代码。
import { Component, OnInit } from '@angular/core';
import { Http, Headers, Response, URLSearchParams } from '@angular/http';
import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
searchValue: any = '';
constructor(private http: HttpClient) { }
getData() {
this.http.post('MY URL',
JSON.stringify({
"q": "Achmea"
}))
.subscribe(
data => {
alert('ok');
console.log(data);
}
)
注意:使用代码 sn-p 因为格式不允许我发布为 pre。
使用最新的 Angular 4 版本。 服务器也应该正确配置,只接受 json 数据。 我尝试了 Angular 文档中的示例,但没有一个有效。
有人知道如何让它工作吗? 提前致谢。
【问题讨论】: