1. 程式人生 > >asp.net core 中Ajax的使用

asp.net core 中Ajax的使用

  • 匯入jquery
    <script src="~/lib/jquery/dist/jquery-3.3.1.js" type="text/javascript"></script>
  •  在js指令碼中寫Ajax

    function FormAjax(){
        $.ajax({
            url: "Register/RegisterResult",
            data: {
                "phone": phone, "verification_code": verification_code,
                "password": password, "password_again": password_again
            },
            type: "post",
            dataType:"text",
            success: function (response) {
                alert(response);
            }
        });
    }
  •  HTML5中使用
    <input id="register_btn" onClick="FormAjax();" type="submit" value="立即註冊">
  •  控制器RegisterController中使用

            public string RegisterResult()
            {
                RegisterViewModel register = new RegisterViewModel();
                register.MobilePhone = Request.Form["phone"];
                register.VerificationCode = Request.Form["verification_code"];
                register.Password = Request.Form["password"];
                register.PasswordAgain = Request.Form["password_again"];
                if (register.MobilePhone!=""&&register.VerificationCode!=""
                    &&register.Password!=""&&register.PasswordAgain!="")
                {
                    return "ok";
                }
                else{
                    return "false";
                }
            }