以下の C# WebApi を POST リクエストで呼び出している場合。メソッドにパラメーターが 1 つある場合は問題なく動作します。別のパラメーターを含めたいとしましょう 。public HttpResponseMessage Post(Member member, bool IsAdmin)
その場合、値は何になりますdata: { id: 2012, firstName: 'FirstNameValue', lastName: 'LastNameValue' }
か?
C#
public HttpResponseMessage Post(Member member)
{
try
{
var id = BusinessModule.PostMember(member);
member.Id = id;
var response = Request.CreateResponse<Member>(HttpStatusCode.Created, member);
response.Headers.Location = new Uri(VirtualPathUtility.AppendTrailingSlash(Request.RequestUri.ToString()) + member.Username);
return response;
}
catch (MemberException e)
{
var response = new HttpResponseMessage(HttpStatusCode.Conflict);
response.Content = new StringContent(e.Message);
throw new HttpResponseException(response);
}
}
Jクエリ
function postMember() {
$.ajax({
url: baseAddress,
type: "POST",
// Firefox reuires the dataType otherwise the "data" argument of the done callback
// is just a string and not a JSON
dataType: 'jsonp',
accept: "application/json",
data: { id: 2012, firstName: 'FirstNameValue', lastName: 'LastNameValue' },
})
.done(function (data) {
$("#membersList").append('<li data-member=\'' + JSON.stringify(data) + '\'>' + data.firstName + ' ' + data.lastName + '</i>');
})
.fail(function (e) {
alert(e.statusText);
})
.always(function () { });
}