简体   繁体   English

Ajax Post-无法获取Post变量

[英]Ajax Post - Unable to get Post variable

codeigniter 2.0 Codeigniter 2.0

form.tpl: form.tpl:

<form class="form-book" id="class-form" method="POST">
  inside full of checkbox....
<button type="button" class="btn" id="submit-btn">Submit</button>
</form>
<script>
$(document).ready(function() {
    $('#submit-btn').click(function(){  
        $('#response').html("<b>Loading response...</b>");
                var serializedData = $form.serialize();
        $.ajax({
            type: 'POST',
            url: 'http://www.example.com/booking/checkbooking', 
            data: serializedData,
        })
        .done(function(data){
            $('#response').html(data);          
        })
        .fail(function() {
            alert( "Posting failed." );

        });
        return false;
    }); 
});
</script>

http://www.example.com/booking/checkbooking : http://www.example.com/booking/checkbooking

public function checkbooking(){
    print_r($_POST);
}

I cant get any post value. 我无法获得任何职位价值。 What have I miss? 我想念什么?

You should to use method: 'POST' not type: 'POST' if you're using a jQuery version larger than 1.9.0 如果您使用的jQuery版本大于1.9.0,则应使用method: 'POST'而不是type: 'POST'

If $_POST is empty, you're either not POSTING it, or it's being emptied and handled elsewhere for security. 如果$_POST为空,则说明您不是在发布它,还是为了安全起见将其清空并在其他地方处理。

You could also check the HTTP Headers and ensure the POST data is actually being sent, click here for instructions 您还可以检查HTTP标头,并确保POST数据已实际发送, 请单击此处获取说明。

type (default: 'GET') Type: String An alias for method. 类型(默认:'GET')类型:字符串方法的别名。 You should use type if you're using versions of jQuery prior to 1.9.0. 如果您使用的是1.9.0之前的jQuery版本,则应使用type。

An alias for method. 方法的别名。

$.ajax({
    method: 'POST',
    url: 'http://www.example.com/booking/checkbooking', 
    data: $('form#class-form').serialize(),
    beforeSend: function() {
            // stuff to do before ajax request is made
    },
    success: function (data) {
            // same as .done()
    },
    error: function() {
            // same as .fail()
    }
});

there is no element like $form, use $('#class-form') instead of $form. 没有像$ form这样的元素,请使用$('#class-form')代替$ form。

<form class="form-book" id="class-form" method="POST">
  inside full of checkbox....
<button type="button" class="btn" id="submit-btn">Submit</button>
</form>
<script>
$(document).ready(function() {
    $('#submit-btn').click(function(){  
        $('#response').html("<b>Loading response...</b>");
                var serializedData = $('#class-form').serialize();
        $.ajax({
            method: 'POST',
            url: 'http://www.example.com/booking/checkbooking', 
            data: serializedData,
        })
        .done(function(data){
            $('#response').html(data);          
        })
        .fail(function() {
            alert( "Posting failed." );

        });
        return false;
    }); 
});
</script>

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

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