webapi获取参数方法大全

多个参数无需自定义类,get 就1种写法,post共2种写法

 

html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <div>
        <form id="form_add">
            <p>id:<input type="text" id="id" name="id" value="123" /></p>
            <p>name:<input type="text" id="name" name="name" value="战神 update 1=1" /></p>
            
            <p>
                <input type="button" value="get" class="btn_get" />
                <input type="button" value="post1" class="btn_post1" />
                <input type="button" value="post2" class="btn_post2" />
            </p>
        </form>
    </div>
    <script type="text/javascript" src="/js/jquery.min_1.8.1.js"></script>
    <script type="text/javascript">
        $(function () {
            //get
            $(".btn_get").click(function () {
                var _data = { id: $("#id").val(), name: $("#name").val() }
                $.ajax({
                    url: '/api/test/getdata1',
                    type: "get",
                    //async: false,
                    data: _data,
                    success: function (data) {
                        console.log("get="+data)
                        alert(data);
                    },
                    error: function () {
                        alert("程序错误");
                    }
                })
            })
            $(".btn_post1").click(function () {
                var _data = { id: $("#id").val(), name: $("#name").val(), sex: 1 }
                $.ajax({
                    url: '/api/test/postdata1',
                    type: "post",
                    //async: false,
                    contentType: 'application/json',
                    data: JSON.stringify(_data),
                    //data: _data,
                    success: function (data) {
                        console.log("post="+data)
                        alert(data);
                    },
                    error: function () {
                        alert("程序错误");
                    }
                })
                return false;
            })
            $(".btn_post2").click(function () {
                var _data = { id: $("#id").val(), name: $("#name").val(),sex:1 }
                $.ajax({
                    url: '/api/test/postdata2',
                    type: "post",
                    //async: false,
                    //contentType: 'application/json',
                    //data: JSON.stringify(_data),
                    data: _data,
                    success: function (data) {
                        console.log("post2=" + data)
                        alert(data);
                    },
                    error: function () {
                        alert("程序错误");
                    }
                })
                return false;
            })
            
        })
    </script>
</body>
</html>

webapi

public class TestController : ApiController
    {

        [HttpGet]
        public string GetData1(string id,string name)
        {
            string res = "id===" + id + ",name===" + name;
            return res;
        }
        [HttpPost]
        public string PostData1(dynamic obj)
        {
            string res = "id===" + obj.id + ",name===" + obj.name;
            return res;
        }
        [HttpPost]
        public string PostData2([FromBody] JObject obj)
        {
            string res = "";
            res += "<br>id===" + obj["id"].ToString() + ",name===" + obj["name"].ToString()+",count="+ obj.Count;
            return res;
        }
    }

 

posted @ 2021-12-18 16:23  WebApi  阅读(711)  评论(0编辑  收藏  举报
CopyRight © 博客园 WebAPI