试图了解我做错了什么。我试图将带有Angular服务的JSON发布到WebAPI2,一旦我通过它,我想在数据库上使用从该JSON获取的参数执行存储过程。
然而,这里发生了什么:
连接已成功解析为Web服务 - 好的,我收到服务器的响应
Web服务上的Debug.WriteLine输出“VAR:0 0”而不是JSON中的实际参数
即使在哪里开始也不确定,Web Service无法处理发送的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;
}
网络服务:
[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();
}
答案 0 :(得分: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)