Hi guys,
at the moment I'm working on a way to configure my Photon local without using the cloud. I just want to make some easy settings like switch the LED type from RGB to RGBW or just W.
Therefor I establish a TCP connection and I created an config.html file which sends GET requests like this on button click:
function loadXMLDoc(str) {
var http = new XMLHttpRequest();
var url = "http://192.168.178.28?" + str;
var params = str;
http.open('GET', url, true);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {
if(http.status == 200) {
window.alert(http.responseText);}
Everything works as expected. I can trace the request with Wireshark. On the Photon side I can simply parse for the parameter and send back an HTTP response like this:
I guess in the response header you should to tell your client that there actually will be some data content by stating the Content-Length in the entity header portion.
You also don’t want an extra blank line after HTTP/1.1 200 OK since that would terminate the header prematurely.
Try
char rspText[] = "Placeholder for Data";
client.println("HTTP/1.1 200 OK"); // mark the lack of \r\n here to avoid early termination
client.println("Content-Type: text/html; charset=UTF-8");
client.printlnf("Content-Length: %d", strlen(rspText));
client.println("\r\n");
The code above does exactly what I want. I can trace the response with Wireshark and see exactly the string as I send it. My problem is how to save this string on the html side using javascript?
Also I changed my load function into this:
function loadXMLDoc(str) {
var url = "http://192.168.178.37?" + str;
var http = new XMLHttpRequest();
http.onreadystatechange = function() {
if (http.readyState === 4) {
window.alert(http.response);}}
http.open('GET', url, true);
http.send();
}
The window.alert(http.response) is executed but it is empty. So there must be an response because the http.readyState is correct. But http.response which I think should contain my string from rspText is still empty. Is there any other way I can solve this?