2016-07-22 109 views
1

我正在嘗試使用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)}); 
+0

415將幾乎肯定是一些由服務器返回。 – Harangue

回答

0

您的代碼只是想發佈字符串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它,然後發送結果作爲帖子的主體。

0

嘗試進入

this._http.post(this.standardUrl, 
      JSON.stringify({ 
       ClientVersion: '1.0.0.0', 
       ClientLanguage: 'en' 
      }), { headers: headers }) 
     .subscribe((response: Response) => { 
       data = response.json(); 
      }); 

這將至少提供一個JSON對象字符串化之前,你的JSON ..

相關問題