1

I wrote an application on my local machine that works perfectly. I am using XAMPP so it is running Apache.

When I uploaded the application to my ubuntu test server, I am getting a 404 error on the ajax call and I am honestly perplexed.

I am using CodeIgniter 3.1.8.

The controller is definitely there and the .htaccess is working fine on my local machine. I checked the case of the filename for the controller and it is fine.

Here's the javascript of the ajax call:

function step2(){
    var canShow = false;    
    var cCode = $("#student_code").val();
    $.ajax({
        url: "/decide/ajaxStep2",
        data: {"student_code": cCode, "lang": "<?=$lang?>"},
        method: "post",
        success: function(data){
        }
    });
}

The controller definition and applicable function (just to show it exists... I don't think the internal code is relevant):

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

class Decide extends MY_Controller {
    function ajaxStep2(){
        //Code here
    }

}

I have done reading and I see some stuff related to the routes file but again I come back to the fact that it works fine on my local machine. I don't think it's file permissions because the index() method runs fine.

I have used ajax a lot and I have never had this issue before so I am honestly not sure where to even look on this. Any thoughts?

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Menachem Bazian
  • 397
  • 2
  • 5
  • 18
  • [**How to debug AJAX calls**](https://stackoverflow.com/a/21617685/2191572) – MonkeyZeus Jun 05 '18 at 19:49
  • 2
    Can you manually visit `http://my-test-ubuntu-server/decide/ajaxStep2` without a 404? There is nothing special about AJAX, it's just a behind-the-scenes web request. – MonkeyZeus Jun 05 '18 at 19:51
  • Does your browser's web dev tool show the requested URL as being correct? – DFriend Jun 05 '18 at 20:15
  • use base_url in ajax URL like url : "=base_url('decide/ajaxStep2');?>" – Pradeep Jun 05 '18 at 23:11
  • MonkeyZeus: I get a 404 when I do it manually... I have checked it out. If I add /index.php/ to the url, it will work from the browser. So, it would seem the problem is the .htaccess file which works perfectly on my local machine. I have checked and the file permissions on the ubuntu are set to 755 on the .htaccess file and it is a standard codeigniter .htaccess file. I have also confirmed that rewrite is loaded so I am a little confused here... – Menachem Bazian Jun 06 '18 at 12:30

3 Answers3

2

Use base_url() or site_url() in ajax like this :

url: "<?=base_url('decide/ajaxStep2');?>",

OR

url: "<?=site_url('decide/ajaxStep2');?>",

Whole code should be like this

function step2()
{
   var canShow = false;    
   var cCode = $("#student_code").val();
   $.ajax({
      url: "<?=base_url('decide/ajaxStep2');?>",
      data: {"student_code": cCode, "lang": "<?=$lang?>"},
      method: "post",
      success: function(data){
      }
   });
}
Pradeep
  • 9,667
  • 13
  • 27
  • 34
1

Could you change the uppercase S from step and try again as lower case url ? I think camel case is been replaced by your framework hope it helps

falcon1812
  • 46
  • 2
  • 4
  • OK. This is weird. I changed everything to lowercase both in the controller and in the JavaScript. It worked once and then stopped (even on the one step that it worked...). I thought this did it but I am back to the drawing board. – Menachem Bazian Jun 06 '18 at 11:59
  • Strange what else did you change on it ? Is the code on this question updated to the last piece ? Maybe replace the uppercase case letter with a dash following by the letter could work (guessing here) something like ```/ajax-step2``` let me know please :) – falcon1812 Jun 06 '18 at 12:34
  • 1
    OK, thanks to some of the comments above I checked the url with a browser. It seems as if the rewrite instructions in the .htaccess are not working and I cannot figure out why. If I change the url to /index.php/decide/ajaxstep2 it works fine (which is what I have done for now). – Menachem Bazian Jun 06 '18 at 13:03
0

As you named your Controller class Decide the file name must be Decide.php. Your application will work perfectly on local XAMPP environment even though you named your file as decide.php but it won't work on a live server.

you have to put the capital letter in front of the file name to make them work. Windows has no problems at all since they don't use case-sensitive naming as Linux/Unix do. So as incredible, this can sound if your Windows XAMPP localhost server is working and when you upload it to a Linux/Unix XAMPP and you keep getting 404 errors, try the capital letter first, that could save you some time.