/home/bradarant/barant/HTTPServer/__script.h
#ifndef ____script_h__
#define ____script_h__
#include "HTTPPage.h"
namespace http {
class __script : public HTTPPage {
int processCommand(std::string request, core::Session *session, HTTPSession *httpSession, HTTPRequest &httpRequest, std::stringstream &data) override {
data << "function serverSend(url, type, receiver, formData, callback) {" << std::endl;
data << " var server = new XMLHttpRequest();" << std::endl;
data << " server.onload = function() {" << std::endl;
data << " if(server.readyState == 4 && server.status == 200)" << std::endl;
data << " callback(server.responseText, receiver);" << std::endl;
data << " };" << std::endl;
data << " server.open(type, url, true);" << std::endl;
data << " server.send(formData);" << std::endl;
data << "}" << std::endl;
data << "function getPage(url, receiver) {" << std::endl;
data << " serverSend(url, \"GET\", receiver, null, function(data, receiver) {" << std::endl;
data << " insertAndExecute(receiver, data);" << std::endl;
data << " });" << std::endl;
data << "}" << std::endl;
data << "function process(url, formName, receiver) {" << std::endl;
data << " var formElement = document.querySelector(\"form[name='\" + formName + \"']\");" << std::endl;
data << " var formData = new FormData(formElement);" << std::endl;
data << " serverSend(url, \"POST\", receiver, formData, function(data, receiver) {" << std::endl;
data << " insertAndExecute(receiver, data);" << std::endl;
data << " });" << std::endl;
data << "}" << std::endl;
data << "function insertAndExecute(id, text) {" << std::endl;
data << " idresolved = document.getElementById(id);" << std::endl;
data << " idresolved.innerHTML = text;" << std::endl;
data << " var script = idresolved.getElementsByTagName(\"script\");" << std::endl;
data << " for(var ix=0;ix<script.length;ix++) {" << std::endl;
data << " eval(script[ix].text);" << std::endl;
data << " }" << std::endl;
data << "}" << std::endl;
httpRequest.response.addHeader("Content-Type", "text/javascript");
return true;
}
};
}
#endif
http::__script
Definition: __script.h:8
http::HTTPSession
Definition: HTTPSession.h:9
http
Definition: __configure.h:4
http::HTTPPage
Definition: HTTPPage.h:11
http::HTTPRequest
Definition: HTTPRequest.h:11