简体   繁体   中英

How to return an object from a webapi httppost controller moethod

I'm trying to return a object from from WebAPI HttpPost controller method but the object isn't getting back to my AngularJS service though the post was successful. The controller method is being called as I can see its results in our database.

The WebAPI method is being called with the following code

erm.service('ERM_ERWeb_MobileSync_Service', ['$http', '$rootScope', 'ERM_Common_Factory', 'ERM_Common_Service',
function ($http, $rootScope, ERM_Common_Factory, ERM_Common_Service) {
    this.SendData = function (payload) {
        console.log('ERM_ERWeb_MobileSync_Service.SendData called...');

        $http.defaults.headers.common.Authorization = 'Basic ' + ERM_Common_Service.Base64Encode(ERM_Common_Factory.GenAuthString());

        $http({
            url: ERM_Common_Factory.GetWebAPILocation() + '/API/MobileSync/ConsumeData',
            dataType: 'json',
            method: 'POST',
            data: payload,
            headers: {
                "Content-Type": "application/json"
            }
        })
        .then(function (response) {
            $rootScope.$emit('ERM_ERWeb_MobileSync_Service_SendData_Complete', {});
        },
        function (error) {
            $rootScope.$emit('ERM_ERWeb_MobileSync_Service_SendData_Error', {});
        });
    };
}]);

This is the controller method.

[HttpPost]
[Route("ConsumeData")]
[ERMobileInstanceAuth]
public IHttpActionResult ConsumeData([FromBody]MobileSyncPayload msp)
{
    try
    {
        _individualService.SetNoShows(msp.NoShows.Select(i => (Models.Data.Instance.T_Individual)i).ToList());
        _flightService.SetFlightTimes(msp.Flights.Select(f => (Models.Data.Instance.T_FlightLog)f).ToList());

        Dictionary<Guid, int> vNums = new Dictionary<Guid, int>();

        foreach (Voucher v in msp.Vouchers)
        {
            Dictionary<Guid, int> vNumsTemp = _voucherService.SyncVouchers(new List<Models.Data.Instance.T_Voucher>() { v });

            vNums = vNums.Concat(vNumsTemp).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

            _voucherLineService.SyncVoucherLines(v.VoucherGUID, v.VoucherLines.Select(vl => (Models.Data.Instance.T_VoucherLine)vl).ToList());
            _cashPaymentService.SyncCashPayments(v.CashPayments.Select(cp => (Models.Data.Instance.T_CashPayment)cp).ToList());
            _cardPaymentService.SyncCardPayments(v.CardPayments.Select(cp => (Models.Data.Instance.T_CardPayment)cp).ToList(), true);

            List<CardPaymentAttempt> acpa = new List<CardPaymentAttempt>();

            foreach (CardPayment cp in v.CardPayments)
            {
                acpa.AddRange(cp.CardPaymentAttempts);
            }

            _cardPaymentAttemptService.SyncCardPaymentAttempts(acpa.Select(cpa => (Models.Data.Instance.T_CardPaymentAttempt)cpa).ToList());
        }

        var returnobject = new { Response = ConvertToList(vNums) };

        return Ok(returnobject);
    }
    catch (Exception e)
    {
        Logging.CreateLogEntry("DataService", Logging.ExceptionHandler.Format(e, Logging.ExceptionHandler.LogLevel.Exception));

        return InternalServerError();
    }
}

There must be something simple I'm missing but I can't see what it is. Anyone got any ideas?

如果我在发射中返回post数据对象,则有帮助

$rootScope.$emit('ERM_ERWeb_MobileSync_Service_SendData_Complete', { Response: response.data.Response });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM