layui发送ajax请求

html:

<form class="layui-form" action="" lay-filter="form-group">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">xxxlabel>
            <div class="layui-input-inline">
              <input type="xxx" name="xxx"  class="layui-input">
            div>
          div>
        div>

        <div class="layui-form-item">
          <label class="layui-form-label">xxxxlabel>
          <div class="layui-input-block">
            <input type="radio" name="111" value="111" title="111" checked="">
            <input type="radio" name="222" value="222" title="222">
          div>
        div>
        <div class="layui-form-item">
          <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="form-submit">提交button>
          div>
        div>
      form>



js:


<script>
form.on('submit(form-submit)', function (inputData) {
        console.log("inputData:",inputData);
        $.ajax({
            url:"xxxxx",
            data:{'xxx':xxx,'xx':xxx},
            type:"post",
            dataType:"json",
        headers : {'Content-Type' : 'application/json;charset=utf-8'}, //接口json格式
            success:function(data){
                console.log(data);
                layer.alert(JSON.stringify(data), {
                    title: data
                });
            },
            error:function(data){
                layer.alert(JSON.stringify(data), {
                    title: data
                });
            }
        });
script>

get:

将post方式改成get方式,url改成对应的地址即可

你可能感兴趣的:(ajax,layui)