码迷,mamicode.com
首页 > Windows程序 > 详细

send js object to webapi or mvc

时间:2015-04-09 17:02:37      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

[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都可以接收到,不用反序列化,亲测可以

send js object to webapi or mvc

标签:

原文地址:http://www.cnblogs.com/bygrace/p/4410016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!