Bolt Iot Training: JavaScript Basics - 2 :- Ajax in Javascript(code)

Hello,
I am doing the training of Bolt IoT.
There is this slight issue for the AJAX code part please check out my code, I also have included what the console in Google is showing.
Please Reply ASAP.
Thank you.

Hi @ajitmec,

Replace the URL https://www.reqres.in/api/users/ with https://reqres.in/api/users/ in the code and try if this is working.

Here is the code for your reference

<!DOCTYPE html>
<html>
<head>
<title>User Data Fetcher</title>
</head>
<body>
<p id="output">Default text</p>
<input id="user_number">
<button onclick="update_user()">Submit</button>
<script type="text/javascript">
function update_user() {
    var user_number = document.getElementById("user_number").value;
    var httpRequest = new XMLHttpRequest();
    httpRequest.open("GET", "https://reqres.in/api/users/" + user_number);
    httpRequest.send();
    httpRequest.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var user_data = JSON.parse(this.responseText);
            document.getElementById("output").innerHTML = user_data.data.first_name;
        }
    }
}
</script>
</body>
</html>

Let us know if you are still facing the issue

1 Like

Thank you so much! @rohit.ts
It worked!