Home > OS >  CodeIgniter ajax link not found
CodeIgniter ajax link not found

Time:02-20

I'm having an issue with getting data from Controller, I have tried all the solutions here but it's the same..

when I click on the button it says 404 not found, if I change the URL to completed URL included the controller class name function name, it says 403

Here is my view code :

<h2>Quiz</h2>
<script type="text/javascript">
$(document).ready(function(){
    var BASE_URL = '<?php echo base_url(); ?>';
    $('#show').click(function(e){
        console.log(BASE_URL);
        $.ajax({
            url: BASE_URL   "Quiz/get_item",
            dataType:'text',
            type:"POST",
            success: function(result){
                var obj = $.parseJSON(result);
                console.log(obj);
            }
        })
    })
});
</script>

<button id="show">Show Cutomers</button>

<div id="customers-list"></div>

<p>Our first multi-page CodeIgniter application! Each page has its own controller and view!</p>

Here is my Controller code :

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

class Quiz extends CI_Controller {

  var $TPL;

  public function __construct()
  {
    parent::__construct();
      $this->load->model('Quiz_data');
    // Your own constructor code
  }
  function show_customers()
    {
    $query = $this->db-> query("SELECT * FROM quiz ORDER BY id;");
    $this->TPL['listing'] = $query->result_array();

    $this->template->show('quiz', $this->TPL);
    }

  public function index()
  {
    $this->template->show('quiz', $this->TPL);
  }
  public function get_item(){
        $data = $this ->Quiz_data->get_data();
        echo json_encode($data);
    }
}

Here is my Model code :

<?php

class Quiz_data extends CI_Model {

        public function get_data()
        {       
                $query = $this->db->get('quiz');
                return $query -> result_array();
        }
}

CodePudding user response:

What is the output of

console.log(BASE_URL);

Didnt mess with CI for quite a while, but it used to need /index.php/ in the URL. Try:

url: BASE_URL   "/index.php/quiz/get_item",

Although the controller-name needs to start with a capital, it doesnt need to be called that way in the URL.

CodePudding user response:

Make sure url: BASE_URL "Quiz/get_item" provides the correct URL.

var BASE_URL = '<?php echo base_url(); ?>'; May not provide trailing slash and you won't get correct url.

Try updating your call with following:

url: BASE_URL   "/Quiz/get_item",
  • Related