php - Ajax access into codeigniter controller

679

I have some problems using my JQuery Ajax methods my controller. Seems that it can´t find the controller, and I can´t seem to get rid of the index.php.

It works fine on my MAMP localhost, but when I uploaded the project til the webhost, apache 2.2.16 (debian). My ajax can't access the controller.

I keep getting this error:

 <title>404 Not Found</title>
 </head><body>
 <h1>Not Found</h1>
 <p>The requested URL /home/stud/xxxxx/public_html/revy/index.php/$ was not found on this
  server.    </p

Ajax, can access and load from files located outside the codeigniter application folder.

This is the error message I get,

My code looks like this:

PHP:

public function loggInn_Ajax(){
    $response = '';
    if($this->input->post('ajax')){
        $this -> form_validation -> set_rules('brukernavn', 'Brukernavn',  
   'trim|required');
        $this -> form_validation -> set_rules('passord', 'Passord', 'required');

        if($this->form_validation->run()){

            $brukernavn = $this->input->post('brukernavn');
            $passord = $this->input->post('passord');
            $aktiveringskode = $this->input->post('aktiveringskode');

            if($aktiveringskode != ''){
                $aktivering = $this->bitauth->activate($aktiveringskode);

                if($aktiveringskode == FALSE){
                    $response = 'Feil aktiveringskode, om du har tastet 
                  riktigkode.Ta kontakt med systemansvarlig.';
                }
            }

            $remember_me = '';

            $res = $this->bitauth->login($brukernavn, $passord, $remember_me);

            if($res === 'inaktiv'){
                $response = 'inaktiv';
            } else if($res == TRUE){
                $response = 'ok';
            }else{
                $response = $this->bitauth->get_error();
            }

        }else{
            $response = validation_errors();
        }
    }
      echo $response;
    }

AJAX:

function loggInn() { 

brukernavn = $('#brukernavn').val();
passord = $('#passord').val();
husk_meg = $('#husk_meg').val();
aktiveringskode = $('#aktiveringskode').val();

sendLoggInnnData();
return false;

}

  function sendLoggInnnData(){

var logginn = {
    'brukernavn': brukernavn,
    'passord': passord,
    'husk_meg': husk_meg,
    'aktiveringskode': aktiveringskode,
    'ajax': 1
};

$.ajax({
    url : 'MetodeKontroller/loggInn_Ajax',
    type: 'POST',
    data: logginn,
    success:function(response) {
        if(response == 'inaktiv'){
            $('#aktiveringsfelt').html("<input name='aktiveringskode' 
       id='aktiveringskode' class='input-block-level' type='text' value='' required='required' 
      placeholder='Aktiveringskode' autofocus='autofocus' onblur='validerInput(this)'    
      maxlength='6' min='6'>");

            $('#aktiveringskode').focus();
        }else if(response == 'ok'){
            window.location.href = "SideKontroller";
        }else{
            $('#alerts').html(alert_error+response+'</div>');
        }
    }

});
    }
990

Answer

Solution:

$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/ProjectFolderName';

Try this

<?php 
var url = '<?php echo base_url();?>';
?>

and in ajax use this:

$.ajax({
    type:'POST',
    url:url+"MetodeKontroller/loggInn_Ajax",

People are also looking for solutions to the problem: php - Internationalisation language switch?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.