0
I have a very strange problem, in case anyone can help, I thank you in advance.
I’m developing an application on Codeigniter 3.1.6 with PHP 7.1 and Bootstrap 4.
The start screen presents a list of students (obtained from a controller called Student, "listing" method). On this page I have a button to register new students in a modal. When I click on it, the modal is displayed correctly.
In the modal I have a button to save the new student in the Mysql database, which makes an Ajax request. The code is as follows:
$('#btnSalvarNovoAluno').on('click', function(){
$.ajax({
url: '<?= base_url() ?>Aluno/inserir',
type: 'POST',
data: $('#frmNovoAluno').serialize(),
dataType: 'json',
success: function(data){
if(data.status)
{
alert('O aluno foi inserido corretamente!');
}
},
error: function(){
alert('Ocorreu um erro ao tentar salvar o aluno.');
}
});
});
In my settings file I put as "base_url":
$config['base_url'] = 'http://localhost:8080/sgicesecbd/';
When I try to save, an error is generated because the application is trying to redirect to
http://localhost:8080/sgicesecbd/Aluno/listagem/<?=%20base_url()%20?>Aluno/inserir
The strange thing is that if I put in the Ajax request the absolute URL (http://localhost:8080/sgicesecbd/Student/insert), works normally.
Has anyone ever been in a similar situation? Thank you in advance!
I was able to solve the problem with the help of the following topic: https://stackoverflow.com/questions/36051588/codeigniter-base-url-didnt-recongnized-in-javascript We needed to create this global variable containing the base_url in the home page header...
Ever tried to change
<?= base_url() ?>
for<?php echo base_url(); ?>
?– NoobSaibot
Yes, but the problem remains the same. Thank you!
– Eduardo Cardoso Melo
You carried
$this->load->helper('url');
– NoobSaibot
Yes, it is loaded in the Student controller constructor.
– Eduardo Cardoso Melo
Carries it in the Autoload ...
$autoload['helper'] = array('url');
inapplication/config/autoload.php
... Try also to give a<?php echo base_url(); ?>
outside the Javascript– NoobSaibot
Also already being loaded in Autoload...
– Eduardo Cardoso Melo
Is this code in a separate js file? Because everything indicates that it is not being processed by PHP. And it’s usually not the case to actually prosecute. I suggest you create a global JS variable in the head of your HTML, and use this variable to mount the URL of the ajax call.
– bfavaretto
That there bfavaretto, is in a separate JS file even. I did it this way and it worked, thank you!
– Eduardo Cardoso Melo
It crossed my mind, except this:
– NoobSaibot
I posted as an answer to get more complete. I hope it helps more people with the same problem or similar.
– bfavaretto