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
FormData from JS
-
- Posts: 12
- Joined: Fri Dec 11, 2020 8:27 am
Re: FormData from JS
I´m using MODM7AE70
Re: FormData from JS
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...
(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
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:
This will just send a normal server okay response. From there, you will trigger the 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
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");
Code: Select all
xhr.status == 200
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