JS | How to retrieve a list of companies

Posted about 1 year ago by Julien Pauthier

Post a topic
Julien Pauthier
Julien Pauthier Admin

You can query a REST API using JavaScript without jQuery (as shown on this Stackoverflow topic). Here is an example to retrieve the list of your locations (or stores) along with opening hours, address and vacations:


<script type="text/javascript">
var xhr = new XMLHttpRequest();
var apiKey = "";
var token = "";
var url = "https://api.agendize.com/api/2.0/scheduling/companies?apikey=" + apiKey + "&token=" + token;
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
        var json = JSON.parse(xhr.responseText);
        document.write("<table>");
        document.write('<thead style="font-weight:bold;background-color:#eee;"><td>Name</td><td>Address</td><td>Opening hours</td><td>Vacations</td></thead>');
        document.write("<tbody>");
        odd = false;
        for (i = 0; i < json.items.length; i++) {
                document.write("<tr");
                if (odd) {      
                        document.write(' style="background-color:#eee;"');
                }
                odd = !odd;
                document.write(">");
                document.write("<td>" + json.items[i].name + " (<a href='https://app.agendize.com/book/" + json.items[i].id + "' target='new'>widget</a>)</td>");
                document.write("<td>" + json.items[i].address.street + " " + json.items[i].address.zipCode + " " + json.items[i].address.city + "</td>");
                document.write("<td>");
                if (json.items[i].workingHours != undefined) {
                        for (j = 0; j < json.items[i].workingHours.length; j++) {
                                for (k = 0; k < json.items[i].workingHours[j].hours.length; k++) {
                                        document.write(json.items[i].workingHours[j].day + ": " + json.items[i].workingHours[j].hours[k].start + " - " + json.items[i].workingHours[j].hours[k].end + "<br/>");
                                }
                        }
                }
                document.write("</td>");
                document.write("<td>");
                if (json.items[i].vacations != undefined) {
                        for (j = 0; j < json.items[i].vacations.length; j++) {
                                document.write(json.items[i].vacations[j].startDate + " - " + json.items[i].vacations[j].endDate + "<br/>");
                        }
                }
                document.write("</td>");
                document.write("</tr>");
        }
        document.write("</tbody>");
        document.write("</table>");
    }
};
xhr.send();
</script>


0 Votes


0 Comments

Login or Sign up to post a comment