Reputation: 15
I've a question, I'm developing a platform with laravel and I'm using ajax, to post and that, the point is that I want to load a blade template after the ajax request ends successfully, how can I do that?
Controller
public function store(Request $request) {
$equipo = new Equipo();
$equipo->id_usuario_crea = $request->id_usuario_crea;
$equipo->consecutivo = $request->consecutivo;
$equipo->nombre_equipo = $request->nombre_equipo;
$equipo->serie = $request->serie;
$equipo->descripcion = $request->descripcion;
$equipo->marca = $request->marca;
$equipo->modelo = $request->modelo;
$equipo->id_empresa = $request->id_empresa;
$equipo->id_sede = $request->id_sede;
$equipo->id_ubicacion = $request->id_ubicacion;
$equipo->id_tipo_equipo = $request->id_tipo_equipo;
$equipo->id_proveedor = $request->id_proveedor;
$equipo->id_estado_equipo = $request->id_estado_equipo;
$equipo->registro_invima = $request->registro_invima;
$equipo->foto = $request->foto;
$equipo->id_riesgo = $request->id_riesgo;
$equipo->save();
if ($request->hasFile('foto') && $equipo->save() == true) {
$path = $request->file('foto')->store('public');
$equipo->foto = $path;
$equipo->save();
}
if($equipo->save() == true){
// $returnHTML = view('equipos.index')->with('user', $user)->render();
$returnHTML = view('equipos.index');
return response()->json(array('success'=> 'El equipo se ha guardado exitosamente', 'html'=>$returnHTML));
//return response()->json(array('success' => true, 'html'=>$returnHTML));
}else{
return response()->json(['fail'=> 'Falló al intentar guardar el equipo']);
}
}
Small part of AJAX
jQuery.ajax({
url: "{{route('equipos.store')}}",
method: 'post',
data: {
consecutivo: consecutivo,
nombre_equipo: nombre_equipo,
serie: serie,
descripcion: descripcion,
marca: marca,
modelo: modelo,
id_empresa: empresa,
id_sede: sede,
id_ubicacion: ubicacion,
id_tipo_equipo: tipo_equipo,
id_proveedor: proveedor,
id_estado_equipo: estado_equipo,
registro_invima: registro_invima,
foto: foto,
id_riesgo: riesgo,
id_usuario_crea: usuario_crea
},
success: function(result) {
console.log(result);
swal({
title: "",
text: result.success,
icon: "success",
button: "Aceptar",
type: "success",
});
**//RETURN THE BLADE TEMPLATE HERE**
},
fail: function(result) {
console.log(result);
sweetAlert("", result.fail, "error");
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
//console.log(ajaxOptions);
console.log(thrownError);
}
});
In the case where the data is saved correctly, I am showing a sweetalert, but I also want to redirect to the "equipos" index view, but I don't know how to do it and I have searched, but, I have not found solution to this, thank you very much in advance.
Upvotes: 1
Views: 540
Reputation: 130
You can return html content from controller ( return view(...) ) and replace html content on ajax.success. ( $('#ajaxResults').html( response.data ) )
Upvotes: 1
Reputation: 360
If you want just redirect the index view, I think you don't need load Html content inside ajax request, just redirect to route with js.
window.location.replace('URL')
In this state URL=route('equipos.index')
Upvotes: 1