0

I am using XAMPP. I am doing this simple implementation of using json and ajax. It always goes to the error part of the ajax and shows this unusual error:

AJAX http://localhost/myajax/employee/showAllEmployee 501 (Not Implemented)

index.php:

<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/bootstrap.min.css') ?>">
        <link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/bootstrap-theme.min.css') ?>">
        <script type="text/javascript" src="<?php echo base_url(); ?>assets/js/jquery-3.1.1.min.js"></script>
        <script type="text/javascript" src="<?php echo base_url(); ?>assets/js/bootstrap.min.js"></script>

    <h2>Employee List</h2>



    <div class="container">
        <button class="btn btn-success">Add New</button>
        <table class="table table-bordered table-responsive" style="margin-top: 20px">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Employee Name</th>
                    <th>Address</th>
                    <th>Created at</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>1</td>
                    <td>Dara</td>
                    <td>Phnom Penh</td>
                    <td>2017</td>
                    <td>
                        <a href="javascript:;" class="btn btn-info">Edit</a>
                        <a href="javascript:;" class="btn btn-danger">Delete</a>
                </tr>
            </tbody>
        </table>

    </div>
    <form>



    </form>

script:

        $(function(){

            showAllEmployee();

            function showAllEmployee(){
                $.ajax({
                    type: 'ajax',
                    url: '<?php echo base_url() ?>employee/showAllEmployee',
                    async: false,
                    dataType: 'json',
                    success: function(data){
                        console.log(data);
                        alert('success');
                    },
                    error: function(){
                        alert('Could not get Data from Database');
                    }
                });
            }


        });
    </script>

controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Employee extends CI_Controller {

    function __construct() {
        parent:: __construct();
        $this->load->model('employee_m', 'm');
    }
    function index(){
        $this->load->helper('url');
        $this->load->view('employee/index');

    }

    public function showAllEmployee() {
        $result = $this->m->showAllEmployee();  
        echo json_encode($result);

    }
}

I have tried using print_r() to check if there is really data from the database, and there is.

guwop69
  • 119
  • 1
  • 13

1 Answers1

2

Change

 type: 'ajax',

To:

 type: 'POST',

Everything else looks alright

Callombert
  • 1,099
  • 14
  • 38
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/501 as it is not a known request method this must be the solution. – qwertzman Feb 22 '17 at 01:34