为什么我的post dall无法在MVC和Angular 7中获取数据

时间:2019-05-20 03:24:31

标签: asp.net-mvc angular asp.net-web-api angular6 angular7

我有下面的代码,我想将角度数据发布到Asp.net MVC,但在对象中为null。方法被调用,但参数为null,不知道为什么??

英语

 var result =  { SearchText: "PARK"};
this.httpClient.post(
'http://localhost:55063/Common/PostAddress',result
).subscribe((res: any[]) => {
              console.log(res);
              this.data = res;
            });

MVC

 public class CommonController : Controller
    {
        protected SCommon sCommon = null;
        public async Task<ActionResult> PostAddress(RoleModel Id)
        {
            sCommon = new SCommon();
            var User = await sCommon.GetAddress(Id.SearchText).ConfigureAwait(false);
            return Json(User, JsonRequestBehavior.AllowGet);
        }
    }

    public class RoleModel
    {
        public string SearchText { get; set; }

    }

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试将标题添加到帖子中,并为您的身体加粗

     const httpOptions = {
          headers: new HttpHeaders({
            "Content-Type": "application/json"

          })
        };

      var result =  { SearchText: "PARK"};  
 const body = JSON.stringify(result);
this.httpClient.post('http://localhost:55063/Common/PostAddress',body,httpOptions).subscribe((res: any[]) => {
          console.log(res);
          this.data = res;
        });

aslo在您的Register方法中启用此类cors

 var cors = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);