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

Why my post dall does not get data in MVC and Angular 7

我有下面的代码,我想 post 从 angular 到 Asp.net MVC 的数据,但在对象中它是空的。方法被调用但参数为空不知道为什么??

ANGULAR

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

    }

尝试将 header 添加到您的 post 并字符串化您的 body

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

请在您的 Register 方法中像这样启用 cors

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