使用 AJAX 显示下拉列表.处理 ajax 不成功请求


using ajax to display dropdown. Handling ajax unsuccess request

我想在火山中得到一个级联下拉列表。我编写了以下 ajax 代码。但它总是显示对话框警报(错误)。那就是 ajax 请求失败。

<script type="text/javascript">
        $(document).ready(function() {
            $('#vehicle_cat_id').change(function() { //any select change on the dropdown with id country trigger this code
                //$("#cities > option").remove(); //first of all clear select items
                var cat_id = $('select#vehicle_cat_id').val(); // here we are taking country id of the selected one.
                //alert(cat_id);
                $.ajax({
                    type: "POST",
                    url: "<?= base_url('admin/vehicle/modellists') ?>", //here we are calling our user controller and get_cities method with the country_id
                    data: 'cat_id = '+cat_id,
                    success: function(html)
                    {
                        //alert('test');
                        $('select#vehicle_model_id').html(html);
                    },
                    error: function(){
                        alert('failed');
                    }
                });
            });
        });
    </script>

我做错了什么?

我做了更正,希望有帮助:

<script type="text/javascript">
        $(document).ready(function() {
            $('#vehicle_cat_id').change(function() { //any select change on the dropdown with id country trigger this code
                //$("#cities > option").remove(); //first of all clear select items
                var cat_id = $('select#vehicle_cat_id').val(); // here we are taking country id of the selected one.
                //alert(cat_id);
                $.ajax({
                    type: "POST",
                    url: "<?= base_url('admin/vehicle/modellists') ?>", //here we are calling our user controller and get_cities method with the country_id
                    data: {
                     'cat_id' : cat_id
}
                    success: function(html)
                    {
                        //alert('test');
                        $('select#vehicle_model_id').html(html);
                    },
                    error: function(){
                        alert('failed');
                    }
                });
            });
        });
    </script>