415不支持的媒体类型angular2
问题描述:
我正在尝试使用angular2 for JavaScript,并且我能够提出Http.get
请求,但是当我试图提出Http.post
请求时,它将返回415 Unsupported Media Type
错误。415不支持的媒体类型angular2
代码本身非常简洁,所以我不确定是什么原因导致了这个错误。我将如何解决这个问题?
var headers = new Headers();
headers.append('Content-Type', 'application/json');
console.log(headers.get('Content-Type'))
return this.http.post(this.endpoint_url, data="test.json", headers=headers).subscribe(
function(response){console.log("Success Response" + response)},
function(error){console.log("Error happened : " + error)});
答
您的代码只是想发布字符串test.json
作为请求的身体,test.json
文件不是内容。
另请注意,您在参数this.http.post()
处使用=
符号。这是赋值给变量并返回它们。
在实践中你的代码是一样的:
return this.http.post(this.endpoint_url, "test.json", headers).subscribe(...
而“test.json”不是有效的JSON字符串(这似乎是你想要的,因为你设置标头)。
如果您想发送test.json
的内容,您应该首先HTTP GET它,然后发送结果作为帖子的主体。
答
尝试进入
this._http.post(this.standardUrl,
JSON.stringify({
ClientVersion: '1.0.0.0',
ClientLanguage: 'en'
}), { headers: headers })
.subscribe((response: Response) => {
data = response.json();
});
这将至少提供一个JSON对象字符串化之前,你的JSON ..
415将几乎肯定是一些由服务器返回。 – Harangue