Dear all,
I would like to change the state of an item from a personal http/javascript webpage.
I have found this function on the forum :
function sendCommand(item, data) {
var urlBase = "http://xx-xx-server/rest/items/"
var url=urlBase + item;
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader("Content-Type", "text/plain");
xhr.setRequestHeader("Accept", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responseText);
};
}
xhr.send(data);
}
do you know the correction of this function to integrate the login / password of openhab 3.0 ?
EDIT :
I try to add :
xhr.setRequestHeader(“Authorization”, "Basic " + btoa(“admin:xxx”));
but finaly I use :
function test() {
fetch('http://xx-xx-server/rest/items/switchNeosmart', {
method: 'POST',
headers: {
'accept': '*/*',
'Content-Type': 'text/plain',
'Authorization': 'Basic' + btoa("admin:xxxxx")
},
body: 'ON'
});
i have found a solution for CORS error message :
conf/services/runtime.cfg : org.openhab.cors:enable=true