ajax提交數據


html代碼

1 <input class="form-control" id="username" name="username" type="text"/>
2 <input class="form-control" id="password" name="password" type="password"/>
3 
4 <button type="button" onclick="loginSubmit()">立即登錄</button>

jquery代碼

function loginSubmit() {

        var username = $("#username").val();    // 獲取id為username的<input>框數據
        var password = $("#password").val();    // 獲取id為password的<input>框數據
        // 判斷
        if (username.length == 0) {
            $("#username").tips({msg: "請輸入用戶名。"});
            return;
        }
        if (password.length == 0) {
            $("#password").tips({msg: "請輸入密碼。"});
            return;
        }
        // Ajax提交數據
        $.ajax({
            url: "admin/check_login",    // 提交到controller的url路徑
            type: "post",    // 提交方式
            data: {"username": username, "password": password},  // data為String類型,必須為 Key/Value 格式。
            dataType: "json",    // 服務器端返回的數據類型
            success: function (data) {    // 請求成功后的回調函數,其中的參數data為controller返回的map,也就是說,@ResponseBody將返回的map轉化為JSON格式的數據,然后通過data這個參數取JSON數據中的值
                if (data.message == "success") {    
                    //跳轉到系統首頁
                   ......
                } else {
                    ......
                }
            },
        });
    }

Ajax中success回調函數: 
success: function(data)是Ajax在請求成功后自動調用的,所以這個方法是Ajax調用的,那么該方法的參數(data)便是Ajax提供的了。其中function(data)的參數data是客戶端請求后台,由后台返回的值。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM