I even tried adding server.close() to the end of each to see if I could force it to say it's done. Still no go....
It was working from jquery with the single function in the example code, but I can't seem to get it to work now that I broke it up. What am I missing?
// should only be called as part of another print function that already sent a header
void listDirJSON(String path, boolean descend) {
server.sendContent("[");
File dir = SD.open((char *)path.c_str());
dir.rewindDirectory();
for (int cnt = 0; true; ++cnt) {
File entry = dir.openNextFile();
if (!entry)
break;
String filename = entry.name();
// skip dot files
if (filename.startsWith("."))
break;
String output = (cnt > 0) ? "," : "";
output += "{name:\"";
output += filename;
output += "\",";
server.sendContent(output);
if (entry.isDirectory()) {
server.sendContent("type:\"dir\"");
if (descend == true) {
server.sendContent(",content:");
String fullpath = path + filename + "/";
listDirJSON(fullpath, descend);
}
} else {
output = "type:\"file\",size:";
output += String(entry.size(), DEC);
server.sendContent(output);
}
server.sendContent("}");
entry.close();
}
dir.close();
server.sendContent("]");
}
void printFS() {
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send(200, "application/json", "");
WiFiClient client = server.client();
server.sendContent("{name:\"/\",type:\"dir\",content:");
listDirJSON("/", true);
server.sendContent("}");
server.close();
}
void printDirectory() {
String path = getDirArg();
if (serverError != "") {
returnFail(serverError);
serverError = "";
return;
}
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
server.send(200, "application/json", "");
String output = "{name:\"";
output += path;
output += "\",type:\"dir\",content:";
server.sendContent(output);
listDirJSON(path, false);
server.sendContent("}");
server.close();
}
example of my ajax jquery call:
console.log("making ajax call"); // I see this
$.ajax({
dataType: "json",
url: "/list",
data: { dir:"/"},
success: function(data) {
console.log("content loaded"); // I never see this
console.log(data);
}
});
EDIT: Initially I set it up to build and return a string of the entire json data but the string was getting huge so converted it back to streaming. But I missed three instances of my trying to do the string handling. One on the declaration of the recurring json sub-function and one each where it was called. I edited those back out in the code and above and still getting the same result.