In: Computer Science
Hi,
Please find the below pages which will show the ajax with the response of HTML and ajax with the response of JSON file data.
Below is the Main Page which I have created:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="https://kendo.cdn.telerik.com/2020.3.915/styles/kendo.common.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2020.3.915/js/kendo.all.min.js"></script>
<script>
$(document).ready(function () {
//To Clear the HTML content
$("#btnClear").click(function () {
$("#ParentTag").html("");
});
//Load the content of HTML from index.html page
$("#btnGetData").click(function () {
$.get("/index.html", function (data, status) {
//all the HTML content will available at this point
$("#ParentTag").html(data);
});
});
//Get the result from json file - Student.json
$("#btnGetJSON").click(function (event) {
$.getJSON('Student.json', function (jd) {
//fetch stud number
$('#studenPreNu').html(jd.studentRefNumber);
var testResult = [];
testResult = jd.testResult;
//Update student difficulty level to * signs
for (var i = 0; i < testResult.length; i++) {
var domElement = testResult[i];
if (domElement.difficultyLevel == 1) {
domElement.difficultyLevel = "*";
}
if (domElement.difficultyLevel == 2) {
domElement.difficultyLevel = "**";
}
if (domElement.difficultyLevel == 3) {
domElement.difficultyLevel = "***";
}
if (domElement.difficultyLevel == 4) {
domElement.difficultyLevel = "****";
}
if (domElement.difficultyLevel == 5) {
domElement.difficultyLevel = "*****";
}
if (domElement.correctAnswer == domElement.yourAnswer) {
domElement.yourAnswer = "Correct";
}
}
//Convert div into kendo grid
$("#dvTable").kendoGrid({
dataSource: testResult,
columns:
[
{
field: "questionNumber",
title: "QuestionNumber"
},
{
field: "content",
title: "Content"
// template: '#= kendo.toString(sales, "N0") #'
}
,
{
field: "topic",
title: "Topic"
// template: '#= kendo.toString(sales, "N0") #'
}
,
{
field: "correctAnswer",
title: "CorrectAnswer"
// template: '#= kendo.toString(sales, "N0") #'
}
,
{
field: "yourAnswer",
title: "YourAnswer"
// template: '#= kendo.toString(sales, "N0") #'
},
{
field: "difficultyLevel",
title: "DifficultyLevel"
}
]
});
var corectAns = $(".k-grid-content tr td").closest("[data-field='yourAnswer']");
for (var i = 0; i < corectAns.length; i++) {
//here we will show the correct image for which user who gives correct answer
if ($(corectAns[i]).html() == "Correct") {
$(corectAns[i]).html(("<span class='glyphicon glyphicon-ok'></span>"));
} else {
//Here is the code which will show the color to grey
var questionNumber = $(".k-grid-content tr td").closest("[data-field='questionNumber']");
$(questionNumber[i]).css("background-color", "grey");
//questionNumber
}
}
});
});
});
</script>
</head>
<body>
<button id="btnGetData">Send an HTTP GET request to a page and get the HTML result back</button>
<button id="btnClear">Clear</button>
<button id="btnGetJSON">Send an HTTP GET request to a JSON data</button>
<div id="ParentTag"></div>
<div id="sudentDetails">
<h1> Test Result. Student Preference Number. <span id="studenPreNu"></span></h1>
<div id="dvTable"></div>
</div>
</body>
</html>
In the above file, we have used index.html and Student.json file. as below:
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<div><h1><span id="headerText">ICE01:</span></h1></div>
<br />
<strong>Ajax/Javascript is working</strong>
</body>
</html>
The above index.html page content will be place in the our main page using ajax
Student.json
{
"studentRefNumber": "BGX8P21R5",
"testResult": [
{
"questionNumber": 1,
"content": "Read a table to solve a problem",
"topic": "Chance & Data",
"correctAnswer": "C",
"yourAnswer": "C",
"difficultyLevel": 1
},
{
"questionNumber": 2,
"content": "Calculate the perimeter of a shape",
"topic": "Measures & Units",
"correctAnswer": "B",
"yourAnswer": "B",
"difficultyLevel": 2
},
{
"questionNumber": 3,
"content": "Solve a word problem involving speed of a vehicle",
"topic": "Algebra & Patterns",
"correctAnswer": "C",
"yourAnswer": "A",
"difficultyLevel": 2
},
{
"questionNumber": 4,
"content": "Solve a word problem involving multiple additions",
"topic": "Algebra & Patterns",
"correctAnswer": "C",
"yourAnswer": "C",
"difficultyLevel": 3
},
{
"questionNumber": 5,
"content": "Identify a shape reflected about a given axis",
"topic": "Space & Geometry",
"correctAnswer": "A",
"yourAnswer": "D",
"difficultyLevel": 5
},
{
"questionNumber": 6,
"content": "Solve a complex problem involving time",
"topic": "Measures & Units",
"correctAnswer": "D",
"yourAnswer": "A",
"difficultyLevel": 3
},
{
"questionNumber": 7,
"content": "Solve a complex problem involving fractions",
"topic": "Number & Arithmetic",
"correctAnswer": "B",
"yourAnswer": "B",
"difficultyLevel": 4
},
{
"questionNumber": 8,
"content": "Solve a complex equation involving two variables",
"topic": "Number & Arithmetic",
"correctAnswer": "C",
"yourAnswer": "B",
"difficultyLevel": 5
},
{
"questionNumber": 9,
"content": "Identify an object shown from a different position",
"topic": "Space & Geometry",
"correctAnswer": "B",
"yourAnswer": "B",
"difficultyLevel": 4
},
{
"questionNumber": 10,
"content": "Translate data table into a graph",
"topic": "Chance & Data",
"correctAnswer": "A",
"yourAnswer": "A",
"difficultyLevel": 1
}
]
}
This JSON data will be show in the Kendo Grid Content in out main page.
Let's see the output:
When you load our page - AjaxWithJSON.html in the browser, it will show
Then I am going to click on the First Button - Send an HTTP GET request to a page and get the HTML result back
You can see that content of index.html page shown in the above page.
Lets click on the button - Send an HTTP GET request to a JSON data
When you click on the Clear button then it will clear the HTML Content.
Thanks.