ajax test

This commit is contained in:
Shawn A
2018-02-02 14:38:40 -06:00
parent 2e08e7de32
commit b1b34a3c97
3 changed files with 34 additions and 0 deletions
+10
View File
@@ -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
*/
+1
View File
@@ -188,6 +188,7 @@ class WiFiManager
void handleNotFound();
void handleExit();
void handleErase();
void handleWiFiStatus();
boolean captivePortal();
boolean configPortalHasTimeout();
+23
View File
@@ -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