Jquery Ajax向服务端传递数组参数值

在选拔MVC时,向服务器端发送POST请求时有时须要传递数组作为参数值

上边选取例子表明,首先看一下Action

[HttpPost]
public ActionResult Test(List<string> model)
{
    return Json(null, JsonRequestBehavior.AllowGet);
}

艺术一,构造表单元素,然后调用serialize()方法得到构造参数字符串

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Test</title>
</head>
<body>
    <div>
        <input type="button" id="btnAjax" value="发送请求" />
    </div>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        var tmp = '<input type="hidden" name="model" value="1" /><input type="hidden" name="model" value="2" />';
        $(function () {
            $("#btnAjax").click(function () {
                $.ajax({
                    url: '@Url.Action("Test")',
                    type: 'POST',
                    data: $(tmp).serialize(),
                    success: function (json) {
                        console.log(json);
                    }
                });
            });
        });
    </script>
</body>
</html>

调试方式监视参数,当点击按钮时,监视获得的参数如下

图片 1

 

图片 2

 

格局二:使用JavaScript对象作为参数传值,参数名是与Action方法对应的参数名,参数值是JavaScript数组

 

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Test</title>
</head>
<body>
    <div>
        <input type="button" id="btnAjax" value="发送请求" />
    </div>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        //var tmp = '<input type="hidden" name="model" value="1" /><input type="hidden" name="model" value="2" />';
        var array = ["abc","123"];
        $(function () {
            $("#btnAjax").click(function () {
                $.ajax({
                    url: '@Url.Action("Test")',
                    type: 'POST',
                    data: {
                        model:array
                    },
                    success: function (json) {
                        console.log(json);
                    }
                });
            });
        });
    </script>
</body>
</html>

图片 3

图片 4

 

措施三,使用Json作为参数请求,此时Ajax须求表明Content-Type为application/json

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Test</title>
</head>
<body>
    <div>
        <input type="button" id="btnAjax" value="发送请求" />
    </div>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        //var tmp = '<input type="hidden" name="model" value="1" /><input type="hidden" name="model" value="2" />';
        //var array = ["abc","123"];
        $(function () {
            $("#btnAjax").click(function () {
                $.ajax({
                    url: '@Url.Action("Test")',
                    type: 'POST',
                    contentType:'application/json;charset=utf-8',
                    data: JSON.stringify({
                        model:["hello","welcome"]
                    }),
                    success: function (json) {
                        console.log(json);
                    }
                });
            });
        });
    </script>
</body>
</html>

图片 5

图片 6

下边的事例使用的是ASP.NET
MVC 5

相关文章