[HttpPost]
public HttpResponseMessage AddInfo(UserInfoEntity userInfo)
{
return Request.CreateResponse(HttpStatusCode.OK,
string.Format("{0},{1},{2}", userInfo.UserName, userInfo.Name, userInfo.PassWord));
}
<script type="text/javascript">
(function () {
var createXhr = function () {
var xhr;
if (window.ActiveXObject) {
xhr = new XMLHttpRequest();
} else {
xhr = new XMLHttpRequest();
}
return xhr;
};
var ajax = function (obj) {
var xhr = createXhr();
xhr.open(obj.type, obj.url, true);
debugger;
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(obj.data);
xhr.onreadystatechange = function () {
//请求成功
if (xhr.readyState == 4 && xhr.status == 200) {
obj.success(xhr.responseText);
}
obj.complete();
};
};
window.common = {
ajax: ajax
};
})(); $("#btnOk").on("click", function () {
common.ajax({
url: "/Default/AddInfo",
type: "post",
dataType: "json",
data: JSON.stringify({
Name: "张三",
UserName: "zhangsan",
PassWord: "123456"
}),
success: function (result) {
alert(result);
},
complete: function () {
}
});
});
</script>

这种方式webapicontroller和mvccontroller都可以接收到,不用反序列化,亲测可以

05-11 11:35