Using Console Log in AJAX – Coding Cyber

Using Console Log in AJAX

We can use console log to log the messages in the console so that we can see these messages in browser. With these console messages, we will know where we are with the code and what’s happening in the background.

Other Articles in Learn AJAX

You can use this code to log messages into the console with responseText

console.log('response from ajax.php : ' + xhr.responseText);

We can do one more thing that is logging the ready states in our ajax function. Use this below code to see all the readystates

function ajax(){
	var xhr = new XMLHttpRequest();
	// Ready State - 0
	console.log('readyState after new : ' + xhr.readyState);'GET', '04-ajax.php',true);
	// Ready State - 1
	console.log('readyState afer open : ' + xhr.readyState);
	xhr.onreadystatechange = function(){
		// Ready State - 2,3,4
		console.log('readyState inside function : ' + xhr.readyState);
		if(xhr.readyState == 4 && xhr.status == 200){
			var text = xhr.responseText;
			console.log('response from ajax.php : ' + xhr.responseText);
			var target = document.getElementById("ajax");
			target.innerHTML = text;



Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: