1. 程式人生 > >ajax實現上傳檔案和form表單上傳檔案的區別

ajax實現上傳檔案和form表單上傳檔案的區別

在使用form表單的時候,一旦點選提交觸發submit事件,一般會使得頁面跳轉,頁面間的跳轉等行為的控制權往往在後端,後端會控制頁面的跳轉及資料傳遞,但是在某些時候不希望頁面跳轉,或者說想要將控制權放在前端,通過js來操作頁面的跳轉或者資料變化。

一般這種非同步的操作,我們都會想到ajax方式,因此在實現了功能後就整理了這篇文章,通過ajax方法實現form表單的提交併進行後續的非同步操作。

常見的form表單提交方式

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>login test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="login test">   
</head>
<body>
<div id="form-div">
    <form id="form1" action="/users/login" method="post">
        <p>使用者名稱:<input name="userName" type="text" id="txtUserName" tabindex="1" size="15" value=""/></p>
        <p>密 碼:<input name="password" type="password" id="TextBox2" tabindex="2" size="16" value=""/></p>
        <p><input type="submit" value="登入">&nbsp<input type="reset" value="重置"></p>
    </form>
</div>
</body>
</html>

點選登入按鈕後,即觸發form表單的提交事件,資料傳輸至後端,由後端控制頁面跳轉和資料。

ajax實現form提交方式

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>login test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="ajax方式">
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript">
        function login() {
            $.ajax({
            //幾個引數需要注意一下
                type: "POST",//方法型別
                dataType: "json",//預期伺服器返回的資料型別
                url: "/users/login" ,//url
                data: $('#form1').serialize(),
                success: function (result) {
                    console.log(result);//列印服務端返回的資料(除錯用)
                    if (result.resultCode == 200) {
                        alert("SUCCESS");
                    }
                    ;
                },
                error : function() {
                    alert("異常!");
                }
            });
        }
    </script>
</head>
<body>
<div id="form-div">
    <form id="form1" onsubmit="return false" action="##" method="post">
        <p>使用者名稱:<input name="userName" type="text" id="txtUserName" tabindex="1" size="15" value=""/></p>
        <p>密 碼:<input name="password" type="password" id="TextBox2" tabindex="2" size="16" value=""/></p>
        <p><input type="button" value="登入" onclick="login()">&nbsp;<input type="reset" value="重置"></p>
    </form>
</div>
</body>
</html>

注意事項

  • 在常用方式中,點選的登入按鈕的type為"submit"型別;
  • 在常用方式中,form的action不為空;
  • ajax方式中需要注意的是$.ajax方法中的引數:dataType和data。

我平時很少寫前端程式碼,級別也就是入門級別,能看懂能改而已,所以很多時候都是百度,像這次這個功能的實現也是藉助了百度,但是,我百度到的程式碼在$.ajax方法中設定的dataType引數值為"html"而不是"json",導致我在一開始除錯的時候一直報錯,最終是改成了"json"才成功,因此在這裡特別說明並提醒一下,別和我一樣走錯了路,還有就是向服務端傳輸的data值了,像上面程式碼一樣,將form表單中的資料序列化傳輸即可。