Database access from index.html template


#1

Is it possible to have read/write access to an external database from the index.html template?


#2

Hi @nigelibrown,

it is possible, but is not implemented by our z.js library, so you would need to do that with JS code.
What external database would you like to access?


#3

We are looking to store a small number of values in a database and access them in the template using, the following. However we have had no success. After some research it would seem that the database has to be on the same server or the browser will not allow access? Can you offer any suggestions?

// POST data to database.
var http = new XMLHttpRequest();
var url = “http://xxx.co.uk/xxx.php”;
var params = "msg=0: "+formatDate(now);
http.open(“POST”, url, true);
http.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
http.setRequestHeader(“Content-length”, params.length);
http.setRequestHeader(“Connection”, “close”);
http.onreadystatechange = function() {//Call a function when the state changes.
if(this.readyState == 4 && this.status == 0) {
alert(http.responseText);
}
}
http.send(params);