0

I have this javascript snippet :

$.ajax({
                type: "Post",
                contentType: 'application/json',
                url: "../api/Pointage/GetPointages",
                 data: JSON.stringify({
                    laDate: DateConsultation,
                    lstcols: Collaborators,
                    lEquipe: equipe,
                    Type: 3,
                }),
                success: function (data) {
                    console.log(data);
                    call3(data);
                }
            });

the signature of the service method is the following :

[HttpPost]
public List<ItemStatistiquesPointageMonth> GetPointages(Nullable<System.DateTime> laDate = null, List<Collaborateur> lstcols =null, Nullable<int> lEquipe = null, int Type = -1)

When I make the call, the serive is unreachable !!

So what is the reason of this problem ? How can I fix it?

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
  • 1
    try `method` instead of `type` - though I thought jquery supports both (depends on jquery version maybe) – Jaromanda X Nov 03 '16 at 10:09
  • 1
    Here, as we seen in the ajax call, you're passing data trough body, but in method have to not declare any `[FromBody]` and even how you can pass multiple parameter in this way? – Divyang Desai Nov 03 '16 at 10:11
  • As @Div pointed out, try creating a model with those properties and having that as the action argument instead. – Phil Cooper Nov 03 '16 at 10:24
  • You can refer [this answer](http://stackoverflow.com/a/40147761/4753489) for reference – Divyang Desai Nov 03 '16 at 10:27

2 Answers2

1

Create a Model with your paramaters and pass it to your post method

[HttpPost]
public List<ItemStatistiquesPointageMonth> GetPointages([FromBody] MyModel model)

also use dataType: "json"

Ahmed
  • 1,542
  • 2
  • 13
  • 21
1

Create a model class which reflect the object you create in the client

public class dataModel
{
    public Nullable<System.DateTime> laDate { get; set; }
    public List<Collaborateur> lstcols { get; set; }
    public Nullable<int> lEquipe { get; set; } 
    public int Type { get; set; } 
}

And then add it to the method with the FromBody attribute

[HttpPost]
public List<ItemStatistiquesPointageMonth> GetPointages([FromBody] dataModel data){}
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69