[GH-ISSUE #1219] Would an AJAX endpoint/callback be useful? #1039

Open
opened 2026-02-28 01:28:14 +03:00 by kerem · 1 comment
Owner

Originally created by @JasXSL on GitHub (Feb 28, 2021).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1219

Was about to make a pull request, but saw you wanted it discussed in an issue first

In our project we use WiFiManager for configuring our device with a random token generated by the ESP. As such we've added a button to the front page that makes an AJAX request to generate a new token, and return this token to be presented to the user. In my fork I added a pretty basic handler that allows you to make requests to /ajax and bind a callback to return a String which is sent as the response. If no callback exists, it returns an empty string.

Example:

Bind a callback before starting the portal:
wifiManager.setAjaxCallback(std::bind(&VhWifi::onAjax, this, _1));

Handle the callback:

String VhWifi::onAjax( WiFiManager* wm ){
    String page = "";

    String task = wm->server->arg(F("t")).c_str();
    if( task == "id" ){
        page = "<your new id>";
    }

    return page;
}

Example of JS injected through setCustomHeadElement:

    fetch('/ajax?t=id')
        .then(res => res.text())
        .then(id => { console.log("Your new ID is", id); });

Should I make a pull request for this, or would you consider this bloat?

Originally created by @JasXSL on GitHub (Feb 28, 2021). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1219 _Was about to make a pull request, but saw you wanted it discussed in an issue first_ In our project we use WiFiManager for configuring our device with a random token generated by the ESP. As such we've added a button to the front page that makes an AJAX request to generate a new token, and return this token to be presented to the user. In my fork I added a pretty basic handler that allows you to make requests to `/ajax` and bind a callback to return a String which is sent as the response. If no callback exists, it returns an empty string. Example: Bind a callback before starting the portal: `wifiManager.setAjaxCallback(std::bind(&VhWifi::onAjax, this, _1));` Handle the callback: ``` String VhWifi::onAjax( WiFiManager* wm ){ String page = ""; String task = wm->server->arg(F("t")).c_str(); if( task == "id" ){ page = "<your new id>"; } return page; } ``` Example of JS injected through `setCustomHeadElement`: ``` fetch('/ajax?t=id') .then(res => res.text()) .then(id => { console.log("Your new ID is", id); }); ``` Should I make a pull request for this, or would you consider this bloat?
Author
Owner

@OldGreyCells commented on GitHub (Mar 1, 2021):

I'm probably well out of line here, but I like the *nix library concept of doing one thing well. To that end, would it be possible for WiFiManager to 'plugin' a webserver class/interface that allows the use of other webserver libraries? eg websockets via ESPAsyncTCP [https://github.com/me-no-dev/ESPAsyncTCP]? I'm currently planning to use that library for app config (and for async MQTT) which means I will have two webserver libraries installed.
Feel free to shout at me!

<!-- gh-comment-id:788086187 --> @OldGreyCells commented on GitHub (Mar 1, 2021): I'm probably well out of line here, but I like the *nix library concept of doing one thing well. To that end, would it be possible for WiFiManager to 'plugin' a webserver class/interface that allows the use of other webserver libraries? eg websockets via ESPAsyncTCP [https://github.com/me-no-dev/ESPAsyncTCP]? I'm currently planning to use that library for app config (and for async MQTT) which means I will have two webserver libraries installed. Feel free to shout at me!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#1039
No description provided.