I have some “Foo” data that I want to transfer from the browser to the server and get predicted statistics based on the information contained in the foo file.
$.ajax({ type: 'GET', url: "/api/predictedStats/", data: "foo=" + ko.toJSON(foo, fooProperties), contentType: 'application/json; charset=utf-8', dataType: 'json', success: function(data) { return _this.viewModel.setPredictedStats(data); }, error: function(jqXHR, statusText, errorText) { return _this.viewModel.setErrorValues(jqXHR, errorText); } });
I created a predicted statistics controller and get a method that takes a Foo argument.
public class PredictedStatsController : ApiController { public PredictedStats Get(Foo foo) { return statsService.GetPredictedStats(foo); } }
Holding a breakpoint in the Get method, I see that the Foo object is always zero. There are no errors caused by the webapi trace log, only the following lines.
WEBAPI: opr[FormatterParameterBinding] opn[ExecuteBindingAsync] msg[Binding parameter 'foo'] status[0] WEBAPI: opr[JsonMediaTypeFormatter] opn[ReadFromStreamAsync] msg[Type='foo', content-type='application/json; charset=utf-8'] status[0] WEBAPI: opr[JsonMediaTypeFormatter] opn[ReadFromStreamAsync] msg[Value read='null'] status[0]
I have no problem sending data through a message to the Foo controller to create a Foo object on the server, so I can say that nothing happened with the json client created.
Looking at the violinist, the resulting Get is as follows, where jsondata is the foo object.
GET /api/predictedStats?foo={jsondata} HTTP/1.1
Is this possible, or am I doing it all wrong?
Thanks neil
EDIT: I feel I almost got this working with the following
public PredictedStats Get([FromUri]Foo foo) { return statsService.GetPredictedStats(foo); }
The foo object returned, but the Foo properties were not correctly populated.
At the same time, I resorted to using POST with almost identical data, just dropping "foo =", and this works fine.
I'm not sure whether to use POST or GET in this case, but that would be interesting to know.
I also found this http://bugs.jquery.com/ticket/8961 , which seems to suggest that you cannot attach the body to a GET request with jquery, so POST is probably the only reasonable option