Angular2服务未将JSON传递给WebAPI [英] Angular2 Service not passing JSON to WebAPI

查看:57
本文介绍了Angular2服务未将JSON传递给WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图了解我在做什么错.我试图将带有Angular服务的JSON发布到WebAPI2,一旦将其传递,我想使用从该JSON中获取的参数在数据库上执行存储过程.

Trying to understand what I am doing wrong. I am trying to POST a JSON with a Angular Service to WebAPI2, once I pass it, I want to execute a stored procedure on the database with parameters taken from that JSON.

但是这里发生了什么

  • 与Web服务的连接已成功解析-好的,我从服务器得到响应

  • The connection is successfully resolved to the Web Service - OK, I get a response from the server

Web服务上的Debug.WriteLine输出"VAR:0 0",而不是JSON中的实际参数

The Debug.WriteLine on the Web Service outputs "VAR: 0 0" instead of the actual parameters in JSON

即使从何处开始也不确定,Web服务是否无法处理发送的JSON或其Angular无法正确传递值的问题?怎么了?

Not sure even where to start, is the problem with the Web Service not being able to handle the JSON sent or its Angular not passing the value correctly? Whats wrong?

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();
        }

推荐答案

删除第一个 content-type ,并在与发送的数据等效的服务参数中更改 name .

Remove the first content-type and change the name in the service parameter equivalent the sent data.

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)

这篇关于Angular2服务未将JSON传递给WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆