ASP.NET WebAPI - how to pass an object with $ .getJSON

I have an ASP.NET WebAPI control below:

public SomeObject GetBenchMarkData(Comment comment)
        {
            //do stuff
        }

On the client side, I try this below:

var comment = { ID: 0, Text: $('#text').val(), Author: $('#author').val(), Email: $('#email').val(), GravatarUrl: '' };
            var json = JSON.stringify(comment);
            $.getJSON("api/MintEIQAPI/" + json,

The problem is that the GetBenchMarkData action is never called using the getJSON request.

Can someone help me what am I doing wrong?

Thank.

+3
source share
2 answers

The problem is that it getJSONis making a request GETto the server. To transfer entire objects you need to execute a requestPOST .

GET JavaScript, jQuery Ajax, URL- , , p >

$.ajax({
  url: "/someurl/getbenchmarkdata",
  data: JSON.stringify({ filterValue: "test" }),
  type: "GET"
  ...

});

public SomeObject GetBenchMarkData(String filterValue)
{
   ...
}

, , ajax POST,

$.ajax({
    url: "/someurl/benchmarkdata",
    type: "POST",
    data: JSON.stringify({ title: "My title"}),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    ...
});

Title String.

.

+6

, GET, FromUri.

:

$.get('api/movies/test',
      {Name:'Henrique', Age:'24'}, 
      function (res) {
           console.log(res);
      }
);

:

public void Get([FromUri] Customer c)
{
    ...
}

, WebApi . , , .

WebApi MVC: http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx

+16

All Articles