I can not call a controller by AJAX in codeIgniter

0

I'm moving my project to production, I was in a develop environment and everything worked correctly (localhost).

When I migrate to production the calls by AJAX do not find the route I need. (Error 404)

EDITO

The variable 'bu' already brings the base_url (), but curiously it is the IP of the server and not the url www.misitio.com (for example)

This is an example that is in JS:

function callAjax( u, m, d = {}, c, a = true ){

    $.ajax({
      url: bu + u,
      method: m,
      async: a,
      data: d,
      beforeSend: function(){

      },
      complete: function(){

      },

      success: c,

      error: function( e ){
        if( e.status >= 400 && e.status < 500){
            renderMessage( 'Servicio no disponible', 'error' );
        }

        else if (e.status >= 500 && e.status < 600) {
          renderMessage( 'Ocurrió un error en el servidor', 'error' );
        }

      }
    });
  }
  callAjax('Items','GET',null,function( r ){});

This is the Items handler

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

class Items extends CI_Controller {

  public function __construct(){
    parent::__construct();
    $this->load->model('items_model');
    $this->load->helper('query');

    $q = $this->items_model->getItems();
    echo queryToJSON( $q );
  }

}
?>

And finally this is my .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>  

I do not know if I have to add the route in the routes.php file or in config, I do not understand if it worked well in develop that is happening in production.

Thanks

    
asked by Alberto Siurob 30.04.2018 в 23:01
source

1 answer

0

I do it this way:

My htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|assets)
#RewriteRule ^(.*) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]

my base_url: link

my Ajax:

var data = $(".formu").serialize();
            $.post('<?php echo base_url() ?>controlador/funcion',data)
            .done(function(resp){
                $(".resultado").html(resp);
            })
            .fail(function(err){
                $(".resultado").html(err);
            })

and everything works wonderfully.

    
answered by 01.05.2018 в 01:49