简体   繁体   English

无法将数据从View发送到Controller

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

can you help about that issue? 您可以为该问题提供帮助吗? I am trying to pass my data to the controller. 我正在尝试将数据传递给控制器​​。 Below you can see my ajax code. 在下面您可以看到我的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>

And my controller is like, 我的控制器就像

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

I checked my data and it is not empty or null. 我检查了我的数据,它不为空或为空。 How can I fix it? 我该如何解决?

Now I am getting this error = 现在我收到此错误=

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

Thanks a lot. 非常感谢。

I had the same problem today during a programming competition. 今天在编程比赛中我遇到了同样的问题。 What solved it for me was using a different way of sending a GET request: 为我解决的是使用另一种发送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
    });
});

Hope it works for you too. 希望它也对您有用。

i thik you should use 我想你应该用

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

instead of 代替

Home/Login

i solve my problem changing the type of ajax as "GET" and changing my controller as "HttpGet" but when I do this for "POST" it didn't solve. 我解决了将ajax类型更改为“ GET”并将控制器更改为“ HttpGet”的问题,但是当我对“ POST”执行此操作时,它没有解决。 Can anyone explain it to me? 谁能向我解释?

 $.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);
            }
        });

i think it is work 我认为这是工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM