代码之家  ›  专栏  ›  技术社区  ›  colt.exe

Ajax不能处理POST表单提交,而是使用默认POST

  •  0
  • colt.exe  · 技术社区  · 3 年前

    我在模态中确实有形式。我试图实现的是重写默认的POST方法并使用AJAX。我这样做是为了防止页面切换。然而,似乎脚本并没有被触发。如果我删除脚本,就会发生正常的POST。代码如下:

    <div class="modal" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="process.php" method="POST" id="submitform">
                <div class="modal-body">
                    <div class="form-group">
                        <label for="source">source:</label>
                        <textarea class="form-control" id="kaynak" name="kaynak" rows="6"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Ä°ptal</button>
                    <button type="submit" class="btn btn-primary ">Kaydet</button>
                </div>
            </form>
        </div>
    </div>
    
        $(document).ready(function() {
    
            $('#submitform').submit(function(event) {
                event.preventDefault();
                var post_url = $(this).attr("action");
                var request_method = $(this).attr("method");
                var formData = $(this).serialize();
                var username = "<?php echo $username ?>";
                formData.push({
                    name: 'username',
                    value: username
                });
    
                $.ajax({
                        type: request_method,
                        url: post_url,
                        data: formData,
                        dataType: 'json',
                        encode: true
                    })
                    .done(function(data) {
                        console.log(data);
                    });
            })
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   Musa Kishore Kumar    3 年前

    你想用 .serializeArray()

            var formData = $(this).serializeArray();
            var username = "<?php echo $username ?>";
            formData.push({
                name: 'username',
                value: username
            });
    
        2
  •  -1
  •   Or Assayag    3 年前

    尝试:

    $(document).ready(function() {
    
        $('#submitform').submit(function(event) {
            event.preventDefault();
            var post_url = $(this).attr("action");
            var request_method = $(this).attr("method");
            var formData = $(this).serialize();
            var username = "<?php echo $username ?>";
            formData['name'] = username;
            $.ajax({
                    type: request_method,
                    url: post_url,
                    data: formData,
                    dataType: 'json',
                    encode: true
                })
                .done(function(data) {
                    console.log(data);
                });
        })