MobileServiceClient.invokeApi导致“在控制器上找不到任何操作”

时间:2015-03-24 09:52:01

标签: azure-mobile-services

我有名为 Foo 的自定义API。当我尝试使用 MobileServiceClient 的javascript库从Html页面调用此自定义方法时,我收到错误:

  

“在控制器'Foo'上找不到与请求匹配的操作”

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/mobileservices/MobileServices.Web-1.2.7.min.js"></script>

< script type = "text/javascript" >
  $(document).ready(function() {
    $('#runAllTests').on('click', function() {
      var serviceClient = new WindowsAzure.MobileServiceClient('<url>', '<key>');
      serviceClient.invokeApi('Foo', {
        body: {
          val1: 'value1',
          val2: 'value2',
        },
        method: 'GET'
      }).done(function(results) {
        console.log('success');
        console.log(results);
      }, function(error) {
        console.log('error');
        console.log(error);
      });

    });
  }); < /script>

但是,当我从Windows应用商店应用中调用此方法时,一切正常。

跨域资源共享(cors)配置为*

更新:看起来我需要调用此方法,使用方法名称指定参数,如:

 serviceClient.invokeApi('Foo?val1=value1&val2=value2', {
   method: 'GET'
 })

这是对的吗?看起来即使对于POST方法我也必须这样做,除非我的参数具有复杂的类型。

更新2:我的自定义API方法的定义如下

[AuthorizeLevel(AuthorizationLevel.Anonymous)]
public class FooController : ApiController
{
     public HttpResponseMessage Get(string val1, string val2)
     {
         //method body
     }
}

1 个答案:

答案 0 :(得分:0)

好的,我发现我需要通过 body 但通过参数传递这些参数。当您通过正文传递它们时,或者至少缺少一个参数时,您将收到“未找到”错误。

serviceClient.invokeApi('Foo', {
  parameters: {
    val1: 'value1',
    val2: 'value2',
  },
  method: 'GET'
})

相关问题