【发布时间】:2020-05-28 16:54:07
【问题描述】:
当从控制器(Codeigniter 4)返回带有数据的视图到 ajax 函数时出现错误 500:
在 Codeigniter 3 中没有这个问题,当 $this->load->view('some'); 但是联合国 CI4 不使用这种方法来显示视图。
在 Codeigniter 4 中:
JS:
<script>
function insertFotosNew(url){
var form = document.querySelector('#new-fotos-form');
var fd = new FormData(form);
$.ajax({
type: "POST",
url: '<?=base_url('subir_fotos')?>',
data: fd,
contentType: false,
processData: false,
success: function (resultado) {
console.log(resultado);
$('#card-fotos').html(resultado);
},
error: function (resultado) {
alert('Se ha producido un error al conectar con el servidor.');
}
});
}
控制器:
/**
* @return View
*/
public function run(){
$todo = $this->request->getPost();
$files = $this->request->getFiles();
$data = array();
if(count($files) > 0) {
$this->respuesta = $this->propiedadNewFotoUpload($todo['id_producto'], $files);
$data['fotos'] = $this->productosFotosRepository->getProductoFotos($todo['id_producto']);
} else {
$this->respuesta->mensaje = 'No hay ficheros para subir';
$this->respuesta->estado = EXIT_ERROR;
}
//return json_encode($this->respuesta);
return view('Administracion/Propiedades/propiedades_form_fotos',$data);
}
}
【问题讨论】:
-
尝试直接加载视图 $this->load->view('Administracion/Propiedades/propiedades_form_fotos',$data);
-
加载资源失败:服务器响应状态为 500 (Internal Server Error) whit $this->load->view('Administracion/Propiedades/propiedades_form_fotos',$data);这是 CI4 而不是 CI3
标签: ajax codeigniter-4