简体   繁体   English

jQuery AJAX发布不起作用Codeigniter

[英]jquery AJAX Post not working Codeigniter

I am currently learning jquery AJAX, i am trying to change my standar php post to AJAX but not working, it's still prosses with php, not AJAX. 我目前正在学习jquery AJAX,我正在尝试将我的标准php帖子更改为AJAX,但无法正常工作,它仍然使用php而不是AJAX来解决问题。 Not even in the console show anything. 甚至没有在控制台中显示任何内容。 Can anyone check my code ? 谁能检查我的代码? I am using Codeigniter. 我正在使用Codeigniter。 And you can asume the php part is working fine .. 并且您可以假设php部分工作正常..

HTML : HTML:

<form action="<?=base_url()?>operator_pt/unggah/unggah_semua" id="unggahsemua" method="POST" name="unggahsemua" role="form">

  <?php  foreach($data as $rod)
  $tahun = $rod->id_smt;
  $jurusan = $rod->id_sms;
    ?>
    <div id="form-tahun" class="form-group">
     <input type="hidden" class="form-control" id="tahun" name="tahun" value="<?php echo $tahun;?>">
     </div>
     <div id="form-jurusan" class="form-group">
     <input type="hidden" class="form-control" id="jurusan" name="jurusan" value="<?php echo $jurusan;?>">       
        </div>
         Sedang Mengunggah :

        <div id="statmhs"> </div>
        <div id="statidmhs"> </div>
        <div id="statdsnpt"> </div>

<i class="fa fa-circle-o-notch fa-spin" style="font-size:24px"></i>   

JS : JS:

$(document).ready(function() {

    $('#unggahsemua').submit(function(event) {

        $('.form-group').removeClass('has-error'); 
        $('.help-block').remove();

        var formData = {
             'tahun'              : $('input[name=tahun]').val(),
            'jurusan'             : $('input[name=jurusan]').val(),
        };

        // process the form
        $.ajax({
            type        : 'POST',
            url         : '<?=base_url()?>operator_pt/unggah/unggah_semua', // the url where we want to POST
            data        : formData, 
            dataType    : 'json', 
            encode      : true
        })

            .done(function(data) {


                console.log(data); 

                $('#statmhs').append('<div class="help-block">' + data.infounggahmhs + '</div>');
                $('#statidmhs').append('<div class="help-block">' + data.infounggahidmhs + '</div>');
                $('#statdsnpt').append('<div class="help-block">' + data.infounggahdsnpt + '</div>');


            .fail(function(data) {


                console.log(data);
            });


        event.preventDefault();
    });

});

You have errors in your Javascript code.. 您的Javascript代码中有错误。

You missed the closing brackets before the .fail method. 您错过了.fail方法之前的.fail括号。 Also you had an extra comma after the last entry of your formData object 另外,在最后一次输入formData对象之后,您还有一个逗号

Try changing to this: 尝试更改为此:

$(document).ready(function () {   
    $('#unggahsemua').submit(function (event) {
        $('.form-group').removeClass('has-error');
        $('.help-block').remove();

        var formData = {
            'tahun': $('input[name=tahun]').val(),
            'jurusan': $('input[name=jurusan]').val()
        };

        // process the form
        $.ajax({
            type: 'POST',
            url: '<?=base_url()?>operator_pt/unggah/unggah_semua', // the url where we want to POST
            data: formData,
            dataType: 'json',
            encode: true
        })    
        .done(function (data) {
            console.log(data);

            $('#statmhs').append('<div class="help-block">' + data.infounggahmhs + '</div>');
            $('#statidmhs').append('<div class="help-block">' + data.infounggahidmhs + '</div>');
            $('#statdsnpt').append('<div class="help-block">' + data.infounggahdsnpt + '</div>');
        })
        .fail(function (data) {
            console.log(data);
        });
        event.preventDefault();
    });
});

You have to stop default execution of form submit as you have to do it with ajax. 您必须停止对表单提交的默认执行,就像您必须使用Ajax一样。

$(document).ready(function () {   
    $('#unggahsemua').submit(function (event) {
      event.preventDefault() //<------- Add this line
        $('.form-group').removeClass('has-error');
        $('.help-block').remove();

        var formData = {
            'tahun': $('input[name=tahun]').val(),
            'jurusan': $('input[name=jurusan]').val()
        };

        // process the form
        $.ajax({
            type: 'POST',
            url: '<?=base_url()?>operator_pt/unggah/unggah_semua', // the url where we want to POST
            data: formData,
            dataType: 'json',
            encode: true
        })    
        .done(function (data) {
            console.log(data);

            $('#statmhs').append('<div class="help-block">' + data.infounggahmhs + '</div>');
            $('#statidmhs').append('<div class="help-block">' + data.infounggahidmhs + '</div>');
            $('#statdsnpt').append('<div class="help-block">' + data.infounggahdsnpt + '</div>');
        })
        .fail(function (data) {
            console.log(data);
        });

    });
});

if you use form action="" method="POST" name="" role="form" , make sure in config.php change $config['csrf_protection'] = TRUE ; 如果使用form action="" method="POST" name="" role="form" ,请确保在config.php更改$config['csrf_protection'] = TRUE to FALSE . FALSE But, if you want to set it TRUE you must use form_open() 但是,如果要将其设置为TRUE,则必须使用form_open()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM