Javascript AngularJS:Promise返回资源

Javascript AngularJS:Promise返回资源,javascript,asp.net-mvc,angularjs,promise,asp.net-web-api,Javascript,Asp.net Mvc,Angularjs,Promise,Asp.net Web Api,存储库方法 public int CalculateSoundVolume(string roomName, int currentUser) { { //Business Logic return finalApplauseVolume; //**say returning 75** } catch (Exception ex) { _logger.

存储库方法

public int CalculateSoundVolume(string roomName, int currentUser)
{

        {

            //Business Logic

            return finalApplauseVolume;  //**say returning 75**

        }
        catch (Exception ex)
        {
            _logger.LogException(ex);
            throw;
        }                                                                                                                          
 }

WEB API控制器

    public IHttpActionResult CalculateSoundVolume()
    {
        try
        {
            //Some Logic 
             var result = _applauseRepository.CalculateSoundVolume(huddleName, currentUser);
            return Ok(result); // **it returns 75 here in result**
        }
        catch (Exception ex)
        {
            _logger.LogException(ex);
            throw;
        }
    }

客户端控制器(ANGULAR JS)


服务

   calculateSoundVolume() 
   {
    return this.soundVolume.get().$promise;        
    }

现在的场景是,我从我的存储库方法返回一个整数值。(说75)。在WEB API控制器中,结果接收到的值为75。
但问题出在我的客户端控制器中的“res”中,我收到的资源为[0]:“7”和[1]:“5”,即未收到实际值和预期值。请提出任何解决方案

同样的问题也发生在我身上。原来$PROMITE不是返回int,而是返回一个对象,该对象将整数的数字拆分为数组中的不同索引,以及PROMITE使用的一些其他信息。我能够通过使用JObject包装整数并从webapi而不是整数传递它来解决这个问题

因此,您的Web API将如下所示:

public JContainer CalculateSoundVolume()
{
    try
    {
        //Some Logic 
         var result = new JObject(new JProperty("soundVolume", _applauseRepository.CalculateSoundVolume(huddleName, currentUser)));
        return result;
    }
    catch (Exception ex)
    {
        _logger.LogException(ex);
        throw;
    }
}
您的客户端控制器将更改为:

public calculateSoundVolume()
{
     var promise = this.applauseService.calculateSoundVolume();
     promise.then((res) => {
         this.$log.debug("Sound Volume : ", res.soundVolume);
});
希望有帮助

--编辑--


我应该澄清一下,我在上述代码中使用的是Newtonsoft库for JSON。我发现了另一个相关的问题。总之,Chandermani是正确的,您应该尝试从服务器发送一个有效的JSON对象。

尝试从服务器发送一个有效的JSON对象,而不是integer,然后看看它是否有效。@Chandermani integer是一个有效的JSON对象。@BenjaminGruenbaum从未尝试发送integer,就像这样,所以我只是让他试试。现在我尝试做一个JSON.stringify(1)很好地实现了密封。谢谢。偶然发现这件事有几个小时了。在我的例子中,我从REST服务返回了一个Java对象,其中只包含我想要返回的整数。
public calculateSoundVolume()
{
     var promise = this.applauseService.calculateSoundVolume();
     promise.then((res) => {
         this.$log.debug("Sound Volume : ", res.soundVolume);
});