Ajax Javascript.html example not working

The given example for ajax is not working . Mainly the onreadystatechange is not being recognized as a function by sublime text editor. Here is a snapshot of the code.

The code is untitled. Save the code with a .html extention. And open it in a browser. Any error will be displayed in the console

Hi @gopakumarvishnu8,

Can you paste your code here instead of a screenshot so that I can test it out? Also, open the console in the browser and check for the error.

I had already saved it but in html format from previous examples. Since it wasn’t still working I tried doing it in a new file. It is still not working even after saving.

Yeah sure . Here it is.

CODE:

Default text

Submit function update_user() { user_number=document.getElementById("user_number").value; httpRequest= new XMLHttpRequest(); httpRequest.open("GET","https://reqres.in/api/users/"+user_number) ; httpRequest.send();
		httpRequest.onreadystatechange = funcion(){
			if(this.readyState==4 && this.status==200)
			{
				user_data=JSON.parse(this.responseText);
				document.getElementById("Output").innerHTML=user_data.data.first_name;
			}
		}
	}
</script>

If you are not facing any errors in the console, try using the tag instead of button.
Also share what error you are facing in the console

I found out that it was a typo , but now I got a new error . Here is a screen shot:

.PS: The previous error was I spelled function wrong.

Hi @gopakumarvishnu8
You have to pass “responseText” as an argument to function. otherwise user_data will be null. That is why you are not able to set it ti html element “output”.

Please ref. below image for Modified code. I was able to get the output:)

Thank you , I got it.

Hello!
The code in the given example is showing some error and is not working. The default text doesn’t change on submitting the user_number.

CODE:

Default Text

Submit

I am hereby attaching the screenshot of the console output.

Please post the code.

Here is the code.

Line 13 has a typo, its supposed to be httpRequest.

oops, My mistake!
Thank you @gopakumarvishnu8


After clicking submit the browser does not show the result.
Please help I have checked code 4-5 times but unable to get the error in code.

Hi @chaitanyasalve14,

There is a typo error in your code. It is this.readyState but you have used this.readystate

Do let me know in case you need any further assistance.

1 Like


Still after hitting submit it dosen’t go forward.


Still the problem is not solved, please help.

Hi @chaitanyasalve14,

I found one more typo error in your code. It is this.responseText but you have used this responseptext.

Please refer the below correct code.

Do let me know in case you need further assistance.

Thank , it’s working now.

1 Like