调用WebAPI的Angularjs控制器总是导致失败回调

时间:2017-09-07 00:48:44

标签: angularjs asp.net-web-api asp.net-web-api2

我有一个WebAPI端点,如代码片段1和AngularJS,如Code Snippet 2.我将代码跟踪到WebAPI,并调用post方法。模型绑定还绑定到请求正文中的正确模型。它还返回OK状态。在Internet Explorer的开发人员工具(F12)的“网络摘要”选项卡中,“结果”列为200,json数据位于“网络详细信息”选项卡的响应正文中。但是,失败回调总是在AngularJS控制器的consultFactory.save()中执行。有谁知道为什么?感谢。

SNIPPET 1



[HttpPost]
public IHttpActionResult Post([FromBody] ConsultRequest consultRequest) {
  if (consultRequest == null)
    return BadRequest("consult request cannot be null");
  var response = TheRepository.GetConsultsWithDetails(consultRequest);

  if (response == null)
    return NotFound();

  response = response
    .OrderBy(r => r.RequestDateTime)
    .Take(consultRequest.Take);

  return Ok(response);
}




SNIPPET 2



function getConsults(consultRequest) {
  $log.log('consultRequest', consultRequest);

  debugger;

  consultFactory.save(consultRequest,
    function(data) {
      $log.log('consult-list.component.getConsults() ::: succeeded in retrieving data from consult factory', data);
      $log.log('consult-list.component.getConsults() ::: total consults', data.length);
      vm.consults = data;
      vm.isBusy = false;
      vm.ActiveCount = ($filter("filter")(data, {
        orderStatus: 'ACTIVE'
      })).length;
      vm.PendingCount = ($filter("filter")(data, {
        orderStatus: 'PENDING'
      })).length;
      vm.PartialResultsCount = ($filter("filter")(data, {
        orderStatus: 'PARTIAL RESULTS'
      })).length;
    },
    function(response) {
      $log.log('consult-list.component.getConsults() ::: failed to retrieve data from consult factory');
      vm.errorMessage = response.statusText + "\r\n";
      vm.isBusy = false;
    });
}




SNIPPET3



(function () {
    'use strict';

    angular.module('consults', ['ngResource'])
        .factory('consultFactory', consultFactory);

    consultFactory.$inject = ['$resource', 'appSettings'];

    function consultFactory($resource, appSettings) {
        return $resource(appSettings.nwicWebApi + "consults/:consultId",
                null,
                {
                    'update': { method: 'PUT', params: {consultId: '@consultId' } }
                });
    };

}());




响应标题 Response Header

0 个答案:

没有答案