JS Ajax in html no output or error shown

I changed some of the variables and id’s but the code is the same, there is no output and there is no error shown in the console of the webpage, if anyone is able to find out any issues in this code please help me out.

@aakashmondal111 there is only a slight mistake in the code.
When you have send an http request to the server so then the server responds back with a staus and the readyState of the request. So readyState of 4 corresponds to Request finsihed and response is ready while the status of 200 refers to Ok.
You have put the correct constraints in the onreadystatechange object’s function. But you have used a wrong keyword, it is onreadystatechange not onreadtstatuschange

Also for being at a safer side replace the id of the para tag to any other variable say test because"nan" may cause an error at backend

1 Like

Thank you a lot, that solved the problem, so is onreadystatechange a predefined function for receiving the request?

The “onreadystatechange” property defines a function to be executed when the readyState changes… The “XMLHttpRequest.onreadystatechange” property contains the event handler to be called when the “readyState” property of the “XMLHttprequest” changes.it is a pre defined function

1 Like
<!DOCTYPE html>
<p id="output"> Default text</p>
    <input id="user_number">
    <button onclick="update_user()">Submit</button>
    <script type="text/javascript">
        function update_user(){
            user_number = document.getElementById("user_number").value;
            httpRequest = new XMLHttpRequest();
            httpRequest.open("GET", "https://reqres.in/api/users"+user_number);

            httpRequest.onreadyStatechange = function(){
                if(this.readyState == 4 && this.status == 200)
                    user_data = JSON.parse(this.responseText);
                    document.getElementById("output").innerHTML = user_data.data.first_name;

My AJAX js code is not running neither it is showing any error. I tried troubleshooting it using given solutions but still i cannot able to detect the mistake. i request to please check it once and tell me the mistake.

@chauhankirti2106 you have done a small typo mistake. In httpRequest object there is a method named as onreadystatechange. So write httpRequest.onreadystatechange and the code will run correctly

i have written it correctly i guess. what changes should i make now?

Write httpRequest.onreadystatechange


Here, @vishalvats2000 means to say is JS is case sensitive, so you have to use the exact cases (lower & upper case letters).

In onreadystatechange all the letters are in lowercase. Make sure to match the changes for any other keyword in the code as well.

Let me know if you have any other queries.

1 Like

I got this. Thankyou!

i am getting the correct output. Thankyou