Undefined Issue in Jquery AJAX

$(document).ready(function(){

	// Ajax request to retrive data
	function showData(){
		$.ajax({
			url:"retrieve.php",
			method:"GET",
			datatype: "json",
			success: function(data){
				// console.log(data);
				if(data){
					x = data;
				}else {
					x = "";
				}
				for(i = 0; i < x.length; i++){
					console.log(x[i].name);
				}
			},
		});
	}
	showData();

Why line#17 is showing as undefined?

When execution gets to the for loop, what is the value of x?

x=data
or
x=" "

That’s not what I meant.

When you actually run the code, what does console.log show is the value of x?

x is never being set as far as I can see (let x; or var x;)

This one →


That means X is set correctly, and we have value in data(=x) object.

P.S. → I have disabled any other console.log

Also ajax is retrieving things from retrive.php:

include 'dbConnection.php';

$sql = "SELECT * FROM studebts";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
	$data = array();
	while ($row = $result->fetch_assoc()) {
		$data[] = $row;
	}
}


// Returning JSON Format
echo json_encode($data);

Problem was datatype vs dataType
Fixed!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.