Anyone know how to get JS to communicate to C#

I am learning how to program in C# and I am trying to create a HTML NUI intractable, The C# script can send messeges easily to the HTML JS but i cant figure out how to get the JS to send info to the C#. If anyone know how please help.

Im using $("#button-close").click(function(){ $.post("http://lspd.net.dll/closeui", JSON.stringify({ event: "closeui" })); });; in my Javascript attached, i have also tried “http://lspd/closeui” instead of what i have above to my html my C# project is called lspd and the .net.dll is LSPD.net.dll.
in my C# Code i have this within my script constructor, Function.Call(Hash.REGISTER_NUI_CALLBACK_TYPE, "closeui"); EventHandlers["closeui"] += new Action<string, object>((data, cb) => { UniformChangerUI.OpenUI(); }); whenever i click my close button nothing happens, please if someone can help that would be much appreciated, I have done hours of googling and searching the forum and i cant seem to find anything that helps.

I have figured it out by looking into other peoples scripts, however i don’t understand it which irritates me. If anyone can point me in the direction to understand why / how it works i would very much appreciate it.

Constructor to register nui callbacks :

private void RegisterNUICallback(string msg, Func<IDictionary<string, object>, CallbackDelegate, CallbackDelegate> callback)
{
API.RegisterNuiCallbackType(msg);
EventHandlers[$"__cfx_nui:{msg}"] += new Action<ExpandoObject, CallbackDelegate>((body, resultCallback) =>
{
CallbackDelegate err = callback.Invoke(body, resultCallback);
});
}

to register a call back call in the scripts constructor :

RegisterNUICallback(“yourcallback”, functionToCall)

function to call :

private CallbackDelegate functionToCall(IDictionary<string,object> data, CallbackDelegate cb) {
//your code you want to run
//end of your code
//not sure why but you need to return CallbackDelegate cb as “ok”
cb("ok);
return cb;
}

In your Javascript you call :

$.post(“http://resourcefilename/any-additional-folders-to-your-script/yourcallback”, JSON.stringify({
// key: value
key1: “value1”,
key2: “value2”, //feel free to use as many keys and values keys can be anything you like
type: “userInterface”,
value: “off”
}));