ajax - 使用来自 ajax 的数据返回视图
问题描述
将带有数据的视图从控制器(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);
}
}
解决方案
你必须回应你的观点,而不是返回它。
echo view('Administracion/Propiedades/propiedades_form_fotos',$data);
去查看文档,CI4 的文档非常好。https://codeigniter.com/user_guide/general/common_functions.html#view
推荐阅读
- python - 关于在 sklearn 中使用 PCA 进行图像检索的困惑
- reactjs - 错误:对象作为 React 子对象无效(找到:[object Promise])
- django - Prefetch_related 多对多
- internet-explorer - Edge IE 模式 - 在 Edge 和 IE 模式之间传递会话
- android - 如果没有加载 url 页面,则加载本地 html (Android Studio)
- string - 如何在 python 中使用 list(intput()) 元素?
- node.js - 如何在 Node.js 中从请求参数获取经过身份验证的用户到 socket.io
- p5.js - 如何绘制褪色的物体轨迹?无需后台操作
- javascript - 开玩笑 - 意外的令牌“出口”
- javascript - 如何将 MongoDB Date.now 从 ISO 字符串转换为 UTC 字符串?