Codeigniter 405方法不允许AJAX JQuery

时间:2018-07-31 07:00:04

标签: php jquery ajax codeigniter

我遇到了问题,我已经使用AJAX为CI页面制作了一个CRUD代码,它在XAMPP服务器上可以正常工作,但是当我将其上传到Live Server(Godaddy)时,它没有从数据库中获取数据并显示405方法不允许错误。

http://www.fenxteksolutions.com/admin/metalinks

这是我的代码。

<script>
    $(function(){
        showAllEmployee();

        //Add New
        $('#btnAdd').click(function(){
            $('#myModal').modal('show');
            $('#myModal').find('.modal-title').text('Add New Meta  Detail');
            $('#myForm').attr('action', '<?php echo base_url() ?>admin/addEmployee');
        });


        $('#btnSave').click(function(){
            var url = $('#myForm').attr('action');
            var data = $('#myForm').serialize();
            //validate form
            var empoyeeName = $('input[name=txtEmployeeName]');
            var address = $('textarea[name=txtAddress]');
            var meta_tag = $('input[name=txtMetaTag]');
            var meta_desc = $('input[name=txtMetaDesc]');
            var result = '';
            if(empoyeeName.val()==''){
                empoyeeName.parent().parent().addClass('has-error');
            }else{
                empoyeeName.parent().parent().removeClass('has-error');
                result +='1';
            }
            if(address.val()==''){
                address.parent().parent().addClass('has-error');
            }else{
                address.parent().parent().removeClass('has-error');
                result +='2';
            }
            if(meta_tag.val()==''){
                meta_tag.parent().parent().addClass('has-error');
            }else{
                meta_tag.parent().parent().removeClass('has-error');
                result +='3';
            }
            if(meta_desc.val()==''){
                meta_desc.parent().parent().addClass('has-error');
            }else{
                meta_desc.parent().parent().removeClass('has-error');
                result +='4';
            }

            if(result=='1234'){
                $.ajax({
                    type: 'ajax',
                    method: 'post',
                    url: url,
                    data: data,
                    async: false,
                    dataType: 'json',
                    success: function(response){
                        if(response.success){
                            $('#myModal').modal('hide');
                            $('#myForm')[0].reset();
                            if(response.type=='add'){
                                var type = 'added'
                            }else if(response.type=='update'){
                                var type ="updated"
                            }
                            $('.alert-success').html('News '+type+' successfully').fadeIn().delay(4000).fadeOut('slow');
                            showAllEmployee();
                        }else{
                            alert('Error');
                        }
                    },
                    error: function(){
                        alert('Could not add data');
                    }
                });
            }
        });

        //edit
        $('#showdata').on('click', '.item-edit', function(){
            var id = $(this).attr('data');
            $('#myModal').modal('show');
            $('#myModal').find('.modal-title').text('Edit Employee');
            $('#myForm').attr('action', '<?php echo base_url() ?>admin/updateEmployee');
            $.ajax({
                type: 'ajax',
                method: 'get',
                url: '<?php echo base_url() ?>admin/editEmployee',
                data: {id: id},
                async: false,
                dataType: 'json',
                success: function(data){
                    $('input[name=txtEmployeeName]').val(data.page);
                    $('textarea[name=txtAddress]').val(data.title);
                    $('input[name=txtMetaTag]').val(data.meta_tag);
                    $('input[name=txtMetaDesc]').val(data.meta_desc);
                    $('input[name=txtId]').val(data.id);
                },
                error: function(){
                    alert('Could not Edit Data');
                }
            });
        });

        //delete-
        $('#showdata').on('click', '.item-delete', function(){
            var id = $(this).attr('data');
            $('#deleteModal').modal('show');
            //prevent previous handler - unbind()
            $('#btnDelete').unbind().click(function(){
                $.ajax({
                    type: 'ajax',
                    method: 'get',
                    async: false,
                    url: '<?php echo base_url() ?>admin/deleteEmployee',
                    data:{id:id},
                    dataType: 'json',
                    success: function(response){
                        if(response.success){
                            $('#deleteModal').modal('hide');
                            $('.alert-success').html('Employee Deleted successfully').fadeIn().delay(4000).fadeOut('slow');
                            showAllEmployee();
                        }else{
                            alert('Error');
                        }
                    },
                    error: function(){
                        alert('Error deleting');
                    }
                });
            });
        });



        //function
        function showAllEmployee(){
            $.ajax({
                type: 'ajax',
                url: '<?php echo base_url() ?>admin/showAllEmployee',
                async: false,
                dataType: 'json',
                success: function(data){

                    var html = '';
                    var i;
                    for(i=0; i<data.length; i++){

                        html +='<tr class="odd gradeX">'+
                                    // '<td>'+data[i].id+'</td>'+
                                    '<td>'+data[i].page+'</td>'+
                                    '<td>'+data[i].title+'</td>'+
                                    '<td>'+data[i].meta_tag+'</td>'+
                                    '<td>'+data[i].meta_desc+'</td>'+
                                    '<td>'+
                                        '<a href="javascript:;" class="btn btn-info item-edit" data="'+data[i].id+'">Edit</a>'+
                                    '</td>'+
                                    '<td>'+
                                        '<a href="javascript:;" class="btn btn-danger item-delete" data="'+data[i].id+'">Delete</a>'+
                                    '</td>'+
                                '</tr>';
                    }
                    $('#showdata').html(html);
                },
                error: function(){
                    alert('Could not get Data from Database');
                }
            });
        }
    });
</script>

此代码在XAMPP Server中运行良好,并且提供了来自数据库的所有数据,并且所有功能均运行良好,但是在托管时,它无法获取数据,当我在控制台中检查页面时,出现405方法不允许错误。

1 个答案:

答案 0 :(得分:4)

您的错误出在ajax的type参数中。删除它或将其设置为GET。实际上是请求方法

 $.ajax({
                type: 'GET',
                url: '<?php echo base_url() ?>admin/showAllEmployee',
                async: false,
                dataType: 'json',
                success: function(data){
.......................