简体   繁体   中英

500 Internal Server Error using Ajax in Codeigniter 3

I am trying to POST some data using ajax to refresh only the content of one tag using this data but I got 500 Internal Server Error when I was debugging.

This is my view code:

    <select id='combolocalidad' name="selectLocalidad" class="form-control">
        <option value="0">Seleccione una localidad:</option>
        <?php
            $i= 0;
            // Realizamos la consulta para extraer los datos
            foreach ($consultalocalidades->result_array() as $fila ):
                $i++;
            // En esta sección estamos llenando el select con datos extraidos de una base de datos.
                        echo '<option value="'.$fila['id_localidad'].'">'.$fila['localidad'].'</option>';
        ?>
        <?php endforeach; ?>
    </select>

This is the ajax code:

$(document).ready(function(){
$('#combolocalidad').on( 'change', function(){ 
    let localidadValue = $('#combolocalidad').val();

    $.ajax({
        type: 'POST',
        url: 'InmueblesController/apiComboLocalidad',
        dataType: "json",
        data: { 
            localidadValue: localidadValue,
        },
        success: function(response){
            console.log(JSON.parse(response));
        }
    })
} );

});

This is the Controller code:

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

class InmueblesController extends CI_Controller {
    
    public function index()
    {
        $dato['consulta']=$this->InmueblesModel->consultar_inmuebles();
        $dato['consultalocalidades']=$this->InmueblesModel->consultar_localidades();
        $dato['consultabarrios']=$this->InmueblesModel->consultar_barrios();
        $this->load->view('head');
        $this->load->view('inmuebles', $dato);
        $this->load->view('footer');
    }

    public function apiComboLocalidad()
    {
        $localidadId = $this->input->post('localidadValue');
        //print_r($_POST);

        if(!empty($localidadId)){
            $dato['consultabarriosById'] = $this->InmueblesModel->consultar_barrios_byId($localidadId);

            $json = array();
            foreach ($consultalocalidades->result_array() as $row ){
                $json[] = array(
                    'id_barrio' => $row['id_barrio'],
                    'barrio' => $row['barrio']
                );
                $i++;
            }
            echo json_encode($json);
        }
    }
}

When I try to print the post request

$localidadId = $this->input->post('localidadValue');

It is empty so the conditional of the controller never runs. And when I change the <select id='combolocalidad' ....> y get the Internal Server Error.

I have tried to send the data like this, but it does not work:

data: { localidadValue }

I have tried to use this, but it does not work:

$localidadId = $this->input->post('localidadValue', true);

Or also:

$localidadId = $this->input->post();

I also tried to use the complete URL using the base_url(), but it does not work.

Please, help me!

The error in this might be because you have a php error in the page you are calling. in your php.ini you should enable display_errors. you can see the error in chrome dev tools > Network tab > find the process you are calling in the list.

Try like this by removing single cotes localidadValue

data: 
    { 
       localidadValue: localidadValue,
    }

It might be help

use

dataType: "json",
data: 
{ 
     localidadValue: localidadValue,
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM