Ajax提交表单初接触

时间:2025-02-23 22:03:32
<!doctype html>
<html class="no-js">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="description" content="">
  <meta name="keywords" content="">
  <meta name="viewport"
        content="width=device-width, initial-scale=1">
  <title>签到test</title>

  <!-- Set render engine for 360 browser -->
  <meta name="renderer" content="webkit">

  <!-- No Baidu Siteapp-->
  <meta http-equiv="Cache-Control" content="no-siteapp"/>

  <!-- Add to homescreen for Chrome on Android -->
  <meta name="mobile-web-app-capable" content="yes">
  <!-- Add to homescreen for Safari on iOS -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="Amaze UI"/>
  <meta name="msapplication-TileColor" content="#0e90d2">

  <!-- SEO: If your mobile URL is different from the desktop URL, add a canonical link to the desktop page https://developers.google.com/webmasters/smartphone-sites/feature-phones -->
  <!--
  <link rel="canonical" href="http://www.example.com/">
  -->

  <link rel="stylesheet" href="assets/css/amazeui.min.css">
  <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
<!--[if lte IE 9]>
<p class="browsehappy">你正在使用<strong>过时</strong>的浏览器,Amaze UI 暂不支持。 请 <a
  href="http://browsehappy.com/" target="_blank">升级浏览器</a>
  以获得更好的体验!</p>
<![endif]-->
<!-- 开始写代码 -->
<!-- banner -->
  <div data-am-widget="slider" class="am-slider am-slider-a1" data-am-slider='{"directionNav":false}' >
    <ul class="am-slides">
        <li>
            <img src="assets/img/banner1.png">
        </li>
        <li>
            <img src="assets/img/banner1.png">

        </li>
    </ul>
  </div>
  <form class="form_box" id="form" action="/users/login">
    <div class="form_group">
      <div class="group_box">
        <span class="am-fl"><i class="icon iconfont"></i></span>
        <input type="text" placeholder="姓名" name="name">
      </div>
    </div>
    <div class="form_group">
      <div class="group_box">
        <span class="am-fl"><i class="icon iconfont"></i></span>
        <input type="text" name="phone" placeholder="电话">
      </div>
    </div>

    <div class="am-form-group">
      <div class="am-u-sm-12 tc">
        <button type="button" class="am-btn am-btn-default" onclick="login()">登录</button>
      </div>
    </div>
  </form>

<!-- 代码结束 -->
<!--[if lt IE 9]>
<script src="http://libs.baidu.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.staticfile.org/modernizr/2.8.3/modernizr.js"></script>
<script src="assets/js/amazeui.ie8polyfill.min.js"></script>
<![endif]-->

<!--[if (gte IE 9)|!(IE)]><!-->
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<!--<![endif]-->
<script src="assets/js/amazeui.min.js"></script>
<script src="assets/js/jquery.validate.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#form').validate({

            rules:{
                user:{
                    required:true,
                    minlength:2,
                },
                phone:{
                    required:true,
                    minlength:11,
                },
            },
            messages:{
                user:{
                    required:'账号不得为空',
                    minlength:'账号不得小于2位',
                    isMobile : true
                },
                phone:{
                     required : "请输入手机号",
                     minlength : "不能小于11个字符",
                     isMobile : "请正确填写手机号码"
                },
            },

        });
    })
    var  name='夏小夏'+'18211013898';//用参数拼接的方法
    //ajax提交表单
    function login() {
            $.ajax({
            //几个参数需要注意一下
                type: "POST",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "" ,//url
                data: {mid:'4900310296747830231',name:name,password:'123456'},//$('#form').serialize(),
                success: function (result) {
                    console.log(result);//打印服务端返回的数据(调试用)
                    if (result.resultCode == 200) {
                        alert("SUCCESS");
                    }
                },
                error : function() {
                    alert("异常!");
                }
            });
        }
</script>

 </body>
</html>

要注意的问题:

1、如果是form提交的话,可以用submit;如果是ajax提交表单用type=button,这样更方便;

2、//$('#form').serialize(),//序列化表单—— 注释的这句不建议使用,不够灵活,因为在ajax需要灵活的提交所需要的参数