FormData from JS

Discussion to talk about software related topics only.
Post Reply
fredy-gutierrez
Posts: 12
Joined: Fri Dec 11, 2020 8:27 am

FormData from JS

Post 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
fredy-gutierrez
Posts: 12
Joined: Fri Dec 11, 2020 8:27 am

Re: FormData from JS

Post by fredy-gutierrez »

I´m using MODM7AE70
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

Re: FormData from JS

Post 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...
User avatar
Jon
Posts: 79
Joined: Mon Feb 05, 2018 10:54 am

Re: FormData from JS

Post 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
Post Reply