Page 1 of 1

FormData from JS

Posted: Wed Apr 21, 2021 9:22 am
by fredy-gutierrez
Hi everyone,
Is there any way to get formData sent from JS to avoid reloading a web page after sending? For example, I do a get using CallBackFunctionPageHandler and this is done correctly, but I can't find the way to post.

For example, for the post I did:

.cpp
int callbackCloseConfig(int sock, HTTP_Request &pHttpRequest)
{
SendHTMLHeader(sock);
//do something
open_close = false;
return 1;
}
CallBackFunctionPageHandler getHandlerClose("close_config.html", callbackCloseConfig, tGet, 0, false);

.js
function httpGetClose(url)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", url, false ); // false for synchronous request
xmlHttp.send( null );
}

Now, I have my function in JS for the post that will send all the data, how do I develop the function that receives that dataform and processes the fields there in my NB?
.js//my send js function
function sendForm() {
var data = new FormData(document.getElementById("fileinfo"));
var xhr = new XMLHttpRequest();
xhr.open("POST", "post_config.html", false)
xhr.send(data);

if (xhr.status == 200) {
//do something
} else {
//do something
}
}

I know that HtmlPostVariableListCallback exists but that would force me to reload the page.
I will appreciate all the possible help

Thanks
Fredy

Re: FormData from JS

Posted: Wed Apr 21, 2021 9:23 am
by fredy-gutierrez
I´m using MODM7AE70

Re: FormData from JS

Posted: Thu Apr 22, 2021 8:15 am
by pbreed
Yes... you can do a http post or request from JS that does not reload the page...
(This is more of a Java Scrip question than a NetBurner question.)
If this post is posting a JSON object you can use the
JsonPostCallbackHandler

To have the ready to decode JSON object presented...
We should probably do an example of doing exactly this...

Re: FormData from JS

Posted: Thu Apr 22, 2021 2:01 pm
by Jon
Hi Fredy,

In the callback handler for HtmlPostVariableListCallback, you can add the following on receiving the eEndOfPost event, instead of the redirect like we show in our examples:

Code: Select all

writestring(sock, "HTTP/1.0 200\r\n\r\n");
This will just send a normal server okay response. From there, you will trigger the

Code: Select all

xhr.status == 200
statement in your JS code, and can proceed as normal from there.

You've probably seen this already, but just for the sake of completeness, the POST processing is documented here:

https://www.netburner.com/NBDocs/Develo ... otoc_md161

Kind Regards,
Jon