user3830715
user3830715

Reputation: 3

Ajax Call With CodeIgniter Using Post

I am using a simple POST to one of my controllers using Ajax, however in my in development tools it seems to never be able to find the controller. I get the error

POST http:// localhost:8888/time.php/checkit 404 (Not Found) 

I'm not sure if this has something to do with the routes of CodeIgniter or the .htaccess file.

My Ajax call looks like:

  $("#submittodo").click(function () {
      $.ajax({
          url: 'time.php/checkit',
          type: "POST",
          data: {name: $(this).val()},
          success: function (data) {
           alert(data);
          }
      });
  });

Upvotes: 0

Views: 639

Answers (3)

Aarranz
Aarranz

Reputation: 461

Yo need to include base_url() on your url:

$("#submittodo").click(function () {
  $.ajax({
      url: '<?php echo base_url()?>time.php/checkit',
      type: "POST",
      data: {name: $(this).val()},
      success: function (data) {
       alert(data);
      }
  });
});

Also, check your config file, if you have $config['csrf_protection'] set to TRUE, you also need to get csrf token like this:

var post_data = {
    'name': $(this).val(),
    '<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'
};

$("#submittodo").click(function () {
  $.ajax({
      url: '<?php echo base_url()?>time.php/checkit',
      type: "POST",
      data: post_data,
      success: function (data) {
       alert(data);
      }
  });
});

Upvotes: 1

Dexter
Dexter

Reputation: 1796

try like this

 $("#submittodo").click(function () {
      $.ajax({
          url: '<?Php echo base_url(); ?>time/checkit',
          type: "POST",
          data: {name: $(this).val()},
          success: function (data) {
           alert(data);
          }
      });
  });

Upvotes: 0

Nil&#39;z
Nil&#39;z

Reputation: 7475

If you are using CI, you need to follow the URL structure, e.g: domain. com/controller/method.c In your case:

URL : '<?Php echo base_url( ) ?>time/checkit',

Mind that there isn't no .Php appended to the controller name. And an absolute URL is used.

Upvotes: 0

Related Questions