使用ajax方法實現form表單的提交

來源:互聯網
上載者:User

標籤:alert   資料   開始   ajax   oct   charset   code   att   reset   

轉http://www.cnblogs.com/han-1034683568/p/7199168.html 寫在前面的話

在使用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表單中的資料序列化傳輸即可。

使用ajax方法實現form表單的提交

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.