Tuximo
Tuximo

Reputation: 57

Angularjs $resource POST send query string not JSON object (Typescript)

When I write a custom action $resource like this:

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,  
        });

and call it from the controller like this:

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

the call is sending like this:

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

The webApi action is accepting entity object as parameter not an id:

[HttpPost]
public Entity Add(Entity entity)

The problem is that it sends the post request with a string parameter (?id=0) and not JSON object.

What am I missing?

Thank you.

Upvotes: 1

Views: 1257

Answers (1)

Simon Sch&#252;pbach
Simon Sch&#252;pbach

Reputation: 2683

Take a look at $resource.

Your problem is, that you pass the data as second parameter. To pass the data as a JSON object you must do the following:

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

Upvotes: 1

Related Questions