Hi,I need to make a synchronous request using ajax from the Client Script of my Edit Page.The purpose is to query my database if the time range of an employee record is being used by the same employee in another record.For this I have created a call in the Api_action, is this:
function Api_Action($app)
{
//Consulta dias en tramite que tiene del periodo de vacaciones
$app->get('/tiempoOcupado/{grupo}/{empresa}/{empleado}/{fechainicio}/{fechafinal}/{permiso}', function ($request, $response, $args) {
// Get input values
$grupo = $args["grupo"];
$empresa = $args["empresa"];
$empleado = $args["empleado"];
$desde = $args["fechainicio"];
$hasta = $args["fechafinal"];
$permiso = $args["permiso"];
if ($ci !== null) {
if ($permiso == 0 ){
$mSql = "";
$mSql = "SET DATEFORMAT YMD; " .
"SELECT CAST(ISNULL(idpermiso,0) AS INT) idpermiso " .
"FROM permisos " .
"WHERE grupocodigo = '" . AdjustSql($grupo) . "' " .
"AND empcodigo = '" . AdjustSql($empresa) . "' " .
"AND emplecodigo = '" . AdjustSql($empleado) . "' " .
"AND (fechainicio BETWEEN '" . AdjustSql($desde) . "' AND '" . AdjustSql($hasta) . "' " .
"OR fechafinal BETWEEN '" . AdjustSql($desde) . "' AND '" . AdjustSql($hasta) . "') " .
"AND estado <> 'RE' " ;
}else{
$mSql = "";
$mSql = "SET DATEFORMAT YMD; " .
"SELECT CAST(ISNULL(idpermiso,0) AS INT) idpermiso " .
"FROM permisos " .
"WHERE grupocodigo = '" . AdjustSql($grupo) . "' " .
"AND empcodigo = '" . AdjustSql($empresa) . "' " .
"AND emplecodigo = '" . AdjustSql($empleado) . "' " .
"AND (fechainicio BETWEEN '" . AdjustSql($desde) . "' AND '" . AdjustSql($hasta) . "' " .
"OR fechafinal BETWEEN '" . AdjustSql($desde) . "' AND '" . AdjustSql($hasta) . "') " .
"AND idpermisso <> " . AdjustSql($permiso) . " " .
"AND estado <> 'RE' " ;
}
//$response = $response->write(ExecuteQuery($mSql)); // Output field value as string
$response = $response->write(ExecuteScalar($mSql)); // Output field value as string
//$response = $response->withJson(ExecuteRow($mSql)); // Output field value as string
}
return $response; // Return Psr\Http\Message\ResponseInterface object
});
}
I make the call to the Api_Action from the Client Script of my Edit Page as follows:
$(document).ready(function() {
$("input[name='x_fechainicio']").blur(function() { // Assume Field1 is a text input
var $row = $(this).fields();
$.ajax({
url: 'api/tiempoOcupado',
type: 'GET',
dataType: 'json',
data: {grupocodigo: $('#x_grupocodigo').val(),
empcodigo: $('#x_empcodigo').val(),
emplecodigo: $('#x_emplecodigo').val(),
fechainicio: $('#x_fechainicio').val(),
fechafinal: $('#x_fechafinal').val(),
permiso: 0},
async:false,
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('a' + textStatus);
}
});
}
But it shows me the following error:XHRGEThttp://localhost:8089/hrmanager/api/tiempoOcupado?grupocodigo=00000001&empcodigo=00000001&emplecodigo=EMP00021&fechainicio=2022/06/30 08:30&fechafinal=2022/06/30 16:30&permiso=0
[HTTP/1.1 401 Unauthorized 162ms]Someone who can guide me by passing me a code fragment of how I should make the call to Api_Action??Thanks,Omar