简体   繁体   中英

ASP.NET MVC 500 (Internal Server Error) with ajax post method

I'm newbie in asp.net mvc, I try to create a post data using ajax, when in the development is run well, but when I try to publish web in server I get the error when post data, the error like this POST https://example.com/login-testing 500 (Internal Server Error) . I try to look for many examples but fail all.

this is my code, may be you can find any problem in my code:

JS script in index.cshtml

function login() {
            var email = $('#input-email').val();
            var password = $('#input-password').val();

            if (email && password) {
                $.ajax({
                    url: '@Url.Action("LoginTesting", "Auth")',
                    type: 'POST',
                    data: JSON.stringify({
                        email: email,
                        password: password
                    }),
                    dataType: 'json',
                    contentType: 'application/json',
                    success: function (data){
                        console.log(data);
                        if (data == 1) {
                            window.location.href = '@Url.Action("Index", "Home")';
                        } else {
                            $('#login-warning').show();
                        }
                    },
                    error: function (data) {
                        $('#login-warning').show();
                    }
                });
            } else if (!email) {
                $('#text-danger-email').show();
            } else if (!password) {
                $('#text-danger-password').show();
            }
        }

controller

[Route("login-testing")]
        public JsonResult LoginTesting(LoginViewModel smodel)
        {
            var email = smodel.email;
            var password = smodel.password;

            DBHandle sdb = new DBHandle();
            var account = sdb.GetLoginVerify(email);
            if (account.password != null)
            {
                if (BCrypt.Net.BCrypt.Verify(password, account.password ))
                {
                    var detail = sdb.GetUserDetail(account.id);
                    if (detail != null)
                    {
                        Session["is_login"] = true;
                        Session["id"] = detail.id;
                        Session["fullname"] = detail.fullname;
                        Session["id_levels"] = detail.id_levels;
                        Session["levels"] = detail.levels;

                        return Json(1);
                    }
                    else
                    {
                        return Json(2);
                    }
                }
                else
                {
                    return Json(3);
                }
            }
            else
            {
                return Json(4);
            }
        }

Please anyone help me to solve this problem.

Thanks.

Internal Server Error probably means something is wrong with your program.cs file.The order in which they are placed is important,improper placements could actually give rise to these errors.

500 internal server also means, there is something wrong with your Code, according to me Go to Chrome Dev Tool and Click on Network Tab, in that Click on XHR tab

  • there your API call must located with red Highlighted text (Since its 500 internal server error ), Click on it, right side window will be appear then click on Preview Tab, you might see which line of Code causing the issue

You can also Debug the Code, and step over code line by line, and check what is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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