mirror of
https://github.com/alexhopeoconnor/WiFiManager.git
synced 2026-10-14 23:21:31 +10:00
ajax test
This commit is contained in:
@@ -250,6 +250,7 @@ void WiFiManager::setupConfigPortal() {
|
||||
server->on("/r", std::bind(&WiFiManager::handleReset, this));
|
||||
server->on("/exit", std::bind(&WiFiManager::handleExit, this));
|
||||
server->on("/erase", std::bind(&WiFiManager::handleErase, this));
|
||||
server->on("/status", std::bind(&WiFiManager::handleWiFiStatus, this));
|
||||
//server->on("/fwlink", std::bind(&WiFiManager::handleRoot, this)); //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
|
||||
server->onNotFound (std::bind(&WiFiManager::handleNotFound, this));
|
||||
|
||||
@@ -827,6 +828,15 @@ String WiFiManager::getParamOut(){
|
||||
return page;
|
||||
}
|
||||
|
||||
void WiFiManager::handleWiFiStatus(){
|
||||
DEBUG_WM(F("<- HTTP WiFi status "));
|
||||
String page;
|
||||
// String page = "{\"result\":true,\"count\":1}";
|
||||
page = FPSTR(HTTP_JS);
|
||||
server->sendHeader("Content-Length", String(page.length()));
|
||||
server->send(200, "text/html", page);
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTPD CALLBACK save form and redirect to WLAN config page again
|
||||
*/
|
||||
|
||||
@@ -188,6 +188,7 @@ class WiFiManager
|
||||
void handleNotFound();
|
||||
void handleExit();
|
||||
void handleErase();
|
||||
void handleWiFiStatus();
|
||||
|
||||
boolean captivePortal();
|
||||
boolean configPortalHasTimeout();
|
||||
|
||||
@@ -68,4 +68,27 @@ const char HTTP_HELP[] PROGMEM =
|
||||
"</table>"
|
||||
"<p/>More information about WiFiManager at <a href='https://github.com/tzapu/WiFiManager'>https://github.com/tzapu/WiFiManager</a>.";
|
||||
|
||||
const char HTTP_JS[] PROGMEM =
|
||||
"<script>function postAjax(url, data, success) {"
|
||||
" var params = typeof data == 'string' ? data : Object.keys(data).map("
|
||||
" function(k){ return encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) }"
|
||||
" ).join('&');"
|
||||
""
|
||||
" var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject(\"Microsoft.XMLHTTP\");"
|
||||
" xhr.open('POST', url);"
|
||||
" xhr.onreadystatechange = function() {"
|
||||
" if (xhr.readyState>3 && xhr.status==200) { success(xhr.responseText); }"
|
||||
" };"
|
||||
" xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');"
|
||||
" xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');"
|
||||
" xhr.send(params);"
|
||||
" return xhr;"
|
||||
"}"
|
||||
""
|
||||
"// example request\n"
|
||||
"postAjax('/status', 'p1=1&p2=Hello+World', function(data){ console.log(data); });"
|
||||
""
|
||||
"// example request with data object\n"
|
||||
"postAjax('/status', { p1: 1, p2: 'Hello World' }, function(data){ console.log(data); });"
|
||||
"</script>";
|
||||
#endif
|
||||
Reference in New Issue
Block a user