2017-01-10 77 views
0

試圖瞭解我做錯了什麼。我想通過一個Angular Service發佈一個JSON到WebAPI2,一旦我通過它,我想用從該JSON取得的參數在數據庫上執行一個存儲過程。Angular2服務沒有通過JSON到WebAPI

然而什麼,而不是在這裏發生的情況:

  • 連接成功解析成Web服務 - OK,我從服務器獲取

  • 上的Web服務輸出中的Debug.WriteLine響應「VAR:0 0」,而不是在JSON中的實際參數

不知道,甚至從哪裏開始,是不能夠到h Web服務的問題併發送JSON或其Angular不正確地傳遞值?怎麼了?

NG2:

updateKnowledge(knowledgeElement: KnowledgeElement) { 
    this._action = "/post"; 

    this._url = CONFIGURATION.baseUrls.server + this._action; 

    let headers = new Headers(); 
    headers.append('Content-Type','application/x-www-form-urlencoded; charset=utf-8'); 
    headers.append('Content-Type','application/json'); 

    this._http 
     .post(this._url,JSON.stringify(knowledgeElement), { headers: headers}) 
        .subscribe((res2) => {console.log('subscribe %o', res2)}); 
    } 

export interface KnowledgeElement { 
    knowledgE_USER_ID: number; 
    knowledgE_NAME: string; 
    knowledgE_DESCRIPTION: string; 
    knowledgE_USER_SCORE: number; 
} 

Web服務:

 [HttpPost] 
     [HttpGet] 
     public IHttpActionResult Post([FromBody]getKnowledgeByUserId_Result value) 
     { 
      var dbContext = new KNOWH_TESTEntities(); 
      System.Diagnostics.Debug.WriteLine("VAR: " + value.KNOWLEDGE_USER_ID + " " + value.KNOWLEDGE_USER_SCORE); 
      dbContext.updateKnowledge(value.KNOWLEDGE_USER_ID, value.KNOWLEDGE_USER_SCORE); 

      return Ok(); 
     } 

回答

1

刪除第一個content-type和改變name服務參數相當於發送數據。

let headers = new Headers(); 
headers.append('Content-Type','application/json'); 

this._http 
    .post(this._url,JSON.stringify(knowledgeElement), { headers: headers}) 
       .subscribe((res2) => {console.log('subscribe %o', res2)}); 

而且Web Api

public IHttpActionResult Post([FromBody]getKnowledgeByUserId_Result knowledgeElement) 
+0

這完美的作品。謝謝。 –