Web API 存在 AJAX 问题/无法加载资源:服务器响应状态为 400 ()

问题描述 投票:0回答:0
[HttpPost("Login")]
        public async Task<IActionResult> Login(SigninViewModel scv)
        {
            MemberCredential membercredential = await db.MemberCredential.FirstOrDefaultAsync(t => t.MemberAccount.Equals(scv.memberAccount));
        
                if (membercredential == null)
                {

                    var test = new { success = false, message = "無此帳號,請重新輸入。" };
                    Console.WriteLine(test);
                    return BadRequest(new { success = false, message = "無此帳號,請重新輸入。" });
                }
                bool isPwdMatch = BCrypt.Net.BCrypt.Verify(scv.memberPassword, membercredential.MemberPassword);
                
                Console.WriteLine("驗證結果:" + isPwdMatch);

                if (isPwdMatch == false)
                {
                    return BadRequest(new { success = false, message = "帳號或密碼錯誤,請重新輸入。" });
                
                }
            
       
                var LoginData = new
                {
                    MemberId = member.MemberId,
                    MemberName = member.MemberName,
                    MemberPoint = member.MemberPoint
                };
                string json = JsonSerializer.Serialize(LoginData);
                Console.WriteLine(json);
                HttpContext.Session.SetString(CDictionary.SK_LOINGED_USER, json);
                
                return Ok(new { success = true, message = "登入成功" });
            
            
        }

输入错误密码,然后执行

return BadRequest(new { success = false, message = "帐号或密码错误,请重新输入。" }); 这段代码,但是ajax并没有执行action。

加载资源失败:服务器响应状态为400()

document.getElementById("Login").addEventListener("submit", function (event) {
            event.preventDefault();

            let MemberAccount = document.getElementById("MemberAccount").value;
            let MemberPassword = document.getElementById("MemberPassword").value;

            const formData=
            {
                memberAccount:MemberAccount,
                memberPassword:MemberPassword
            }

            $.ajax({
                type:"POST",
                url: "/api/Members/Login",
                data:JSON.stringify(formData),
                contentType: "application/json"
            }).done(data=>{
                if (data.success) {
                    alert("登入成功");
                    window.location.href = "https://localhost:5000/Home/Index";
                } else {
                    alert("登入失敗!");
                }
        }).fail((jqXHR, textStatus, errorThrown) => {
            console.error("AJAX 發生錯誤:", textStatus, errorThrown);
        });;

    });

请帮我找出问题所在 关于BadRequest,都没有实现,ajax没有动作

关于return BadRequest()的动作有执行,但是ajax并没有做出动作。在开发人员介面上出现了无法加载资源:服务器响应状态为400 () 请我帮忙修改上面的程序代码,谢谢!

javascript c# ajax asp.net-core-webapi
© www.soinside.com 2019 - 2024. All rights reserved.