【问题标题】:Angularjs $resource POST send query string not JSON object (Typescript)Angularjs $resource POST 发送查询字符串不是 JSON 对象(Typescript)
【发布时间】:2016-05-17 17:04:43
【问题描述】:

当我像这样编写自定义操作 $resource 时:

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) 而不是 JSON 对象发送 post 请求。

我错过了什么?

谢谢。

【问题讨论】:

  • 您是否遇到任何错误?有什么问题?
  • 它发送的是查询字符串 /add?id=0 而不是 JSON 对象。

标签: angularjs typescript angular-resource


【解决方案1】:

看看$resource

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

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

【讨论】:

    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    相关资源
    最近更新 更多