Reputation: 45155
I have a method in my web api controller that looks something like this:
[HttpPost]
public string DoRun(Guid id, IDictionary<string,object> parameters)
{
string val = this.Request.Content.ReadAsStringAsync().Result;
return val;
}
And the problem I am having is that I can't get the parameters
to actually bind. It does attempt to read it from the body of the request because val
is an empty string, but the Count
of parameters
is zero (removing the parameters
argument entirely, you can see the data in val
and it'll echo it back as a json string).
Here's how I'm posting (which might also be the problem):
var dummyPayload = {
regions: [111, 222, 333],
code: 55661,
private: true,
level: "foo"
};
$.ajax({
url: "../../api/MyController/DoRun?id=7EEB1650-1CB7-41DB-8C0B-1016ACA0E568",
type: "POST",
data: JSON.stringify(dummyPayload),
dataType: "json"
}).done(function (d) {
console.log(d);
});
Any ideas what I'm doing wrong?
Upvotes: 0
Views: 2203
Reputation: 19311
Specify the media type of your request body using contentType
. dataType
sets the Accept
header and contentType
sets Content-Type
.
$.ajax({
url: "../../api/MyController/DoRun?id=7EEB1650-1CB7-41DB-8C0B-1016ACA0E568",
type: "POST",
data: JSON.stringify(dummyPayload),
dataType: "json",
contentType: "application/json",
}).done(function (d) {
console.log(d);
});
Upvotes: 1