I got below code and I want to use ajax response out side of function but it continuously shows me undefined, I know it is easy but how to handle it?
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>test</title>
<script>
var sourceData;
var xhttp;
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
sourceData = this.responseText;
//
}
};
xhttp.open("GET", "http://localhost:34560/test/js/source.json", true);
xhttp.send();
document.getElementById("test").innerHTML=sourceData;
</script>
</head>
<body>
<div id="test"></div>
</body>
</html>
Update:
I do not want to use ajax inside function as you can see it is inside script tag.