【问题标题】:Unable to get the result from web api action called from angular无法从 angular 调用的 web api 操作中获取结果
【发布时间】:2019-07-02 07:06:58
【问题描述】:

我试图通过在 Angular 中通过 POST 请求向 WebApi 传递用户的电子邮件来获取 UserProfile 详细信息,但我无法获取数据。我收到此错误:输入意外结束。

这是调用的方法:

getUserProfile(resourceEmail: string): Observable<any> {
debugger
return this.http.post<UserVM>(this.getAPI('FetchUserProfile')+'/? emailId='+resourceEmail,
  this.httpOptions).pipe(
    map(data => data),
    catchError(this.handleError<any>())
  );
 }

控制器动作代码

    [System.Web.Mvc.HttpPost]
    [System.Web.Mvc.Route("ApiWithAction")]
    public HttpResponseMessage FetchUserProfile(string emailId)
    {
        try
        {
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, _userProfileService.GetUserProfile(emailId));
            return response;
        }
        catch (Exception ex)
        {
            //client.TrackException(ex.InnerException);
            return null; //need to check and confirm what needs return to UI               
        }
    }

订阅代码

 getBookingList() {
this.bookingListSubscription = this.restService.getCompletedListForGrid(this.booking).subscribe(
  data => {

    this.bookingList = data;

    this.bookingList.forEach((element) => {
      element.StartDate = new Date(element.StartDate),
        element.EndDate = new Date(element.EndDate);
    });
    this.gridData = this.bookingList;
    this.spinner.hide();
  });

【问题讨论】:

  • 发布您与订阅相关的代码
  • @Pradeep Jain 我已经用订阅代码更新了问题
  • 你试过我的建议了吗?
  • 是的,我试过但得到同样的错误
  • 你能创建同样的 stackblizt 吗?

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


【解决方案1】:

可能你以错误的方式分配一个值,最好分配一个值 -

this.restService.getUserProfile("")
 .subscribe(data => {
    this.bookingListSubscription
    .... other code
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 2018-10-18
    • 1970-01-01
    • 2012-04-26
    • 2021-10-28
    • 2022-08-13
    • 1970-01-01
    相关资源
    最近更新 更多