I like to improve my code. I want to load the html form data from a json file by using ajax.
here is my code:
<script type="text/javascript" >
function getFile(path, success) {
var req = new XMLHttpRequest();
req.open("GET", path, true);
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status === 200) {
success(req.responseText); // call a function with the received value
} else {
console.log("unexpected response status " + req.status);
}
}
};
req.send(null);
}
function load() {
getFile('config.json', function (allText) {
var configdata = JSON.parse(allText);
document.getElementById("devicename").value = configdata.devicename;
document.getElementById("mqtt_host").value = configdata.mqtt_host;
document.getElementById("mqtt_username").value = configdata.mqtt_username;
document.getElementById("mqtt_pass").value = configdata.mqtt_pass;
document.getElementById("mqtt_port").value = configdata.mqtt_port;
document.getElementById("mqtt_ssl").value = configdata.mqtt_ssl;
});
}
</script>
any points to improve??
file looks like:
{
"devicename": "t",
"mqtt_host": "z",
"mqtt_username": "z",
"mqtt_pass": "t",
"mqtt_port": "7",
"mqtt_ssl": "65"
}
Thanks