簡體   English   中英

無法將數據從View發送到Controller

[英]Can't sending data from View to Controller

您可以為該問題提供幫助嗎? 我正在嘗試將數據傳遞給控制器​​。 在下面您可以看到我的Ajax代碼。

<script type="text/javascript">
    $(document).on("click", "#login_button", function () {

        var userName = document.getElementById("userName").value;
        var password = document.getElementById("password").value;

        if (userName !== "" && password !== "") {

            $.ajax({

                url: '/Home/Login',
                type: 'POST',
                contentType: 'application/json; charset=utf-8',
                data: {
                    'userName' : userName,
                    'password' : password
                },
                datatype: 'json',
                success: function (data) {
                }


            })
        }
        else {
            alert("Lütfen Alanları Doldurunuz.")

        }
    })


</script>

我的控制器就像

   [HttpPost]
    public ActionResult Login(string userName, string password)
    {
        return View();
    }

我檢查了我的數據,它不為空或為空。 我該如何解決?

現在我收到此錯誤=

jquery.min.js:4 POST http://localhost:59277/Home/Login 500 (Internal Server Error)

非常感謝。

今天在編程比賽中我遇到了同樣的問題。 為我解決的是使用另一種發送GET請求的方式:

    $.get("URL/to/send/request/to", function(data, status){
        alert("Data: " + data + "\nStatus: " + status);//data is the actual response you get, while status is weather the request was successful
    });
});

希望它也對您有用。

我想你應該用

 url: 'http://stackoverflow.com/Home/Login',

代替

Home/Login

我解決了將ajax類型更改為“ GET”並將控制器更改為“ HttpGet”的問題,但是當我對“ POST”執行此操作時,它沒有解決。 誰能向我解釋?

 $.ajax({
            url: 'http://stackoverflow.com/Home/Login',
            type: "POST",
            dataType: 'json',
            data: dataToPost,

            contentType: "application/json; charset=utf-8",
            success: function (data) {
                alert("hi" + data);
            }
        });

我認為這是工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM