Page 1 of 1

401 error in a custom api

Posted: Mon Jul 08, 2024 10:12 am
by Cat

hello everyone

I have a 401 error in a custom api

I have the code in Api_Action

$app->get('/getChatByName/{nombre}', function ($request, $response, $args) {
        $name = $args["nombre"] ?? null; // Get the input value
        if ($name !== null) {
        
          //  $response = $response->write(ExecuteScalar("SELECT chat_id FROM usuarios WHERE nombre = '" . AdjustSql($name) . "'")); // 
            $response = $response->write(ExecuteScalar("SELECT chat_id FROM usuarios WHERE nombre = " . AdjustSql($name))); // Output field value as string
        }
        return $response; // Return Psr\Http\Message\ResponseInterface object
    });

}

and in the table specify client_scrips

$("#x_DESTINATARIO").change(function() {
    $.get(ew.getApiUrl(["getChatByName", $(this).val()]), function(res) {
        if (res)
            $("#x_ChatID").val(res);  // Set the result (manipulate it first if necessary) to the target field
    });
});

I am logging in as administrator

Failed to load resource: the server responded with a status of 401 (Unauthorized)

the users table is not the security table

phpmaker 2024.13


Re: 401 error in a custom api

Posted: Mon Jul 08, 2024 10:26 am
by arbei

Make sure $(this).val() has value or the URL does not meet '/getChatByName/{nombre}'.


Re: 401 error in a custom api

Posted: Thu Jul 11, 2024 1:43 am
by DGarza

Cat wrote:

$("#x_DESTINATARIO").change(function() {
    $.get(ew.getApiUrl(["getChatByName", $(this).val()]), function(res) {
        if (res)
            $("#x_ChatID").val(res);  // Set the result (manipulate it
first if necessary) to the target field
    });
});

Befere the condition add console.log(res) for see what it get and see the value