[英]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.我正在尝试使用 ajax 发布一些数据,以仅使用此数据刷新一个标签的内容,但在调试时出现 500 内部服务器错误。
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:这是ajax代码:
$(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.当我更改 <select id='combolocalidad' ....> 时,您会收到内部服务器错误。
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.我还尝试使用 base_url() 使用完整的 URL,但它不起作用。
Please, help me!请帮我!
The error in this might be because you have a php error in the page you are calling.这个错误可能是因为您正在调用的页面中有一个 php 错误。 in your php.ini you should enable display_errors.在您的 php.ini 中,您应该启用 display_errors。 you can see the error in chrome dev tools > Network tab > find the process you are calling in the list.您可以在 chrome 开发工具 > 网络选项卡 > 在列表中找到您正在调用的进程中看到错误。
Try like this by removing single cotes localidadValue
通过删除单个localidadValue
尝试这样localidadValue
data:
{
localidadValue: localidadValue,
}
It might be help这可能有帮助
use用
dataType: "json",
data:
{
localidadValue: localidadValue,
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.