0

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();
        }
}
Alex
  • 1
  • Maybe below link will help
    https://stackoverflow.com/questions/50934092/403-forbidden-error-using-ajax-in-codeigniter
    – sajjad anwar Feb 19 '22 at 22:27
  • I already looked at it as it's False by default – Alex Feb 19 '22 at 22:45
  • could you please post the complete error message? You find it in your browser's Network->Response tab – Vickel Feb 20 '22 at 00:18
  • @Vickel .. Thanks. here is the error 404 Not Found

    Not Found

    The requested URL was not found on this server.


    Apache/2.4.51 (Win64) PHP/7.4.26 Server at 127.0.0.1 Port 80
    – Alex Feb 20 '22 at 00:22
  • @Vickel idk if you seen my response? – Alex Feb 20 '22 at 00:36
  • ok and that's for `get_item`? Again, I'm talking about the network tab of your browser. Also: you can always try a relative path like `url: "/quiz/get_item",`. BTW, if you have an edit to your question, edit your question and not in the comment section. Comments are not meant to be necessary to understand your Question, they are used to calrify... – Vickel Feb 20 '22 at 00:36
  • @Vickel Hi there, okay this is from the network->response when I click on the button, as there is no error before clicking the button, also I tried to use your pathway and the same. – Alex Feb 20 '22 at 00:47
  • 2
    this might be a mess-up due to the template stuff you are setting up, I'd strip it down to just a "normal" ajax call and check in the controller if you have arrived there, something like `die('here I am');` and then add-on from there. ALSO check if the model's and the controller's file naming: if they start with the first letter as Uppercase... – Vickel Feb 20 '22 at 00:58

2 Answers2

0

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",
erekle
  • 88
  • 1
  • 7
0

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.

fido128
  • 1
  • 2