2016-05-17 70 views
0

当我写了一个自定义操作$资源这样的:

getEntityResource(): ng.resource.IResourceClass<IEntityResource> { 


     let addAction: ng.resource.IActionDescriptor = { 
      method: 'POST', 
      url: 'http://localhost:8085/api/entity/add' 
     } 
     return <ng.resource.IResourceClass<IEntityResource>> 
     this.$resource("http://localhost:8085/api/entity/:entityId", { id: '@id' }, { 
      add: addAction, 
     }); 

,并从控制器这样称呼它:

this.$mdDialog.hide(this.dataService 
      .getEntityResource() 
      .add(this.entity, 
      () => this.$state.reload() 
     )); 

该呼叫是这样发送的:

Request URL:http://localhost:8085/api/entity/add?id=0 

webApi操作正在接受实体对象作为参数不是ID:

[HttpPost] 
public Entity Add(Entity entity) 

问题是它发送带有字符串参数(?id = 0)的post请求而不是JSON对象。

我错过了什么?

谢谢。

+0

您是否收到任何错误?有什么问题? –

+0

它发送一个查询字符串/ add?id = 0而不是JSON对象。 – Tuximo

回答

0

看看$resource

你的问题是,你传递的数据作为第二个参数。要将数据作为JSON对象传递,您必须执行以下操作:

$resource("http://localhost:8085/api/entity/:entityId", 
    {}, 
    {params: {id: '@id'}...} 
);