shuhelohelo’s blog

Xamarin.Forms多めです.

ASP.NET MVCでAjaxでPOST

環境

動いたコードを残す.

配列をPOSTして,同じ配列を受け取る.

Controller

    public class ProductsController : Controller
    {
        [HttpPost]
        public ActionResult GetProduct(string[] names)
        {
            return Json(names);
        }
    }

View

<button id="myButton">ボタン</button>

<script>
    $('#myButton').on('click', function () {
        console.log('clicked');

        $.ajax({
            type: "POST",
            url: "/Products/GetProduct",
            data: { names: ["a", "b", "c"] },
            async: false,
            success: function (ret) {
                console.log(ret);
            },
            error: function (e) { }
        });
    });
</script>