在将简单类型传递给Web API操作时,我知道special requirements(单个参数,空键名,使用FormBody
)。但我无法使用jQuery $.ajax
和http PUT
。该操作已被点击,但值始终为null
。
控制器的行动是:
public class ValuesController: ApiController
{
public void Put([FormBody]string value)
{
}
}
javascript是:
$.ajax({
type: 'put',
contentType: 'application/json',
url: 'Values/',
data: {'': '1,newValue'},
});
答案 0 :(得分:0)
这有效:
[HttpPut]
public void Put(person value)
{
}
public class person
{
public string name { get; set; }
public string surname { get; set; }
}
$("#clickme").click(function () {
var person = new Object();
person.name = "Homer";
person.surname = "Simpson";
var value = person;
$.ajax({
url: '/api/blah',
type: 'PUT',
dataType: 'json',
data: person,
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});