Nui callback not working?

Hello everyone,
I recently started trying to use nui for my resources I tried to use the nui callback and well it failed I tried to make a nui bill the displaying was ok everything worked but when I tried to use the buttons (I added the option to Decline or accept it) it done jack in good terms so said anyway the short version is it should return the value of what button I pressed (They are either “declined” or “accepted”)
2023-07-11 01_18_28-FiveM® by Cfx.re - Tara-me _ ESX Legacy Server
it should work but it doesn’t does anyone got a idea how to fix it?

index.html (the script function):

function sendResponse(action) {
            console.log('Sending response: ' + action);
            console.log(`http://${GetParentResourceName()}/response`);
            $.post(`http://${GetParentResourceName()}/response`, {
                METHOD: `POST`,
                headers: {
                    'Content-Type': 'application/json; charset=UTF-8',
                },
                body: JSON.stringify({
                    action: action,
                }),
            });
        }

client.lua (function):

function CallBack()
    RegisterNUICallback('response', function(data, cb)
        cb('ok')
        return data.action
    end)
end

client.lua (where it is called):

local reuslt = CallBack()
while result == nil do
    Citizen.Wait(0)
    result = CallBack()
end
print(result)

Thanks in Advance!

P.S. I have never used nui before I tried to work with the documentation but I am failing miserly

That is not really how you would do that.
Seems like you are overcomplicating it quite a lot.

This would suffice as your client.lua code:

RegisterNUICallback('response', function(data, cb)
    cb('ok')
    print(data.action)
end)

The reason returning isn’t working would be because you arent returning in the correct function. If you really want to return in that way, then something more like this would be what you want:

function CallBack()
    local prom = promise.new()
    RegisterNUICallback('response', function(data, cb)
        cb('ok')
        prom:resolve(data.action)
    end)

    Citizen.Await(prom)
    return prom.value
end
local result = CallBack()
print(result)

Although in my opinion, handling nui callbacks in such way would really not be ideal.

Hello thanks for the reply I tried it but sadly it didn’t worked I override the CallBack function with the code you gave me and added the result

                                            OpenBillPage(plate, buyPrice, km, lostWorth, taxes, finalPrice)
                                            result = CallBack()
                                            local reuslt = CallBack()
                                            print(result)

(It is inside a script I need it for like accepting the deal for selling the vehicle) the nui page outputs the whole time it sends the response but I don’t receive any on the client side :confused:

But again thanks again for the fast reply!

I would recommend that you just register the nui callback normally it would be both easier and more readable. + There is no real need nor reason for why you would need to do it that way.

You are overcomplicating it quite a lot by trying to use an callback inside a callback.

I’ve registed normally with this code you gave me but I still didn’t receive any Callback :confused:

Hey if you’re using fx_version ‘cerulean’, change the http to a https in $.post

Thank you so much it worked!

me falla esto alguie me puede ayudar error during NUI callback confirmSelected: Execution of function reference in script host failed: