AJAX Javacrupt.html example not working(Windows OS)

@siddharthoza00. , you have almost written the code perfectly. But in javaScript whenever we do an AJAX call then we get the response from the server.
The responseText property has the response in the form of a string. So you have to parse that responseText in order to fetch a particular detail.

In the line 22, you have misspelled the spelling of responseText. Correct it and the code will work fine

2 Likes

Thank you, that was very helpful.

I am glad I helped you. So now please do close the query by clicking on the Solution button on my previous post so that this query can be closed :slight_smile:

please check my code! I am not getting the output.

Hi @sunnysrikar.944,

Please paste your code here instead of sharing the screenshot. You can </> from the editor tool to insert your code. It will help us in resolving your query.

There are 2 changes as of now -

  1. closing the function
httpRequest.onreadystatechange = function (responseText){
.
.
.
.
};

  1. this.State --> this.status

Do the changes first. If you dont get any output again, take a screenshot of console tab (Right click on browser > Inspect > Console) while you click the ‘SUBMIT’ button. And send it over here.

here is the screenshot of my output and code @akshayan.sinha

<body>
<p id="output" >Default Text</p>
<input id="user_number">
<button onclick="user_update()">Submit</button>
  <script type="text/javascript">
   function user_update(){
     user_number=document.getElementById("user_number").value;
        httpRequest= new XMLHttpRequest();
        httpRequest.open("GET","https://reqres.in/api/users/"+user_number);
        httpRequest.send();
        httpRequest.onreadystatechange = function(responseText){
         if(this.readyState==4 && this.Status==200)
         {
          user_data = JSON.parse(this.responseText);
          document.getElementById("output").innerHTML=user_data.data.first_name;
        };
      }
   }
  </script>
this is the code for body in the ajax example @rahul.singh1

this.status not this.Status

1 Like

thankyou! its working now:) @akshayan.sinha @rahul.singh1

2 Likes