2016-07-22 7 views
1

JavaScript 용 angular2를 사용해 본 결과 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() 인수에 = 기호를 사용하고 있는지 확인합니다. 그것은 vars에 값을 지정하고 리턴합니다. 실제로

은 당신의 코드와 동일합니다 :

return this.http.post(this.endpoint_url, "test.json", headers).subscribe(... 

그리고 "test.json"(때문에 당신이 설정하는 헤더에, 당신이 원하는 것 같다) 유효한 JSON 문자열이 아닙니다.

당신이 test.json의 내용을 보내려면

먼저 HTTP 그것을 얻을해야은 다음 게시물의 본문으로 결과를 보냅니다. 항목

적어도 JSON 객체를 제공합니다
this._http.post(this.standardUrl, 
      JSON.stringify({ 
       ClientVersion: '1.0.0.0', 
       ClientLanguage: 'en' 
      }), { headers: headers }) 
     .subscribe((response: Response) => { 
       data = response.json(); 
      }); 

전에 JSON을 stringifying

관련 문제