How do you send a JSON string to another page using ajax get method? As a prac question we need to do this.
Once the questions are answered, the user's name, age, questions and results (Hint: you may store both the correct answer and the user's answer and do the comparison in the next task, but at minimum store a boolean to indicate whether the user got the answer correct) need to be stored with JSON. The string needs to be sent over GET to the summary page as a parameter. This time, use jQuery's AJAX. You may use $.get(...) for 5 marks, however, for the full 10 marks, use $.ajax(...).
I have tried everything but cannot get anyting to work.
function storeResults()
{
/*var name = getUrlVars()["name"];
var age = getUrlVars()["age"];
var percent = correctAns / 6 * 100;
details = {"name": name, "age": age, "percent": percent};
var questions = {"q1": question1, "q2": question2, "q3": question3, "q4": question4, "q5": question5, "q6": question6};*/
//localStorage.setItem("detail", JSON.stringify(details));
var theObject = { p1: "v1", p2 : "v2" };
var jqxhr =
$.ajax({
url: "summary.html",
processData : false,
type : "GET",
data: JSON.stringify(theObject)
})
.done (function(data) { $('#ajaxDiv').html(data) })
.fail (function() { alert("Error ") ; });
}
function getResults()
{
/*var obj = JSON.parse($_GET["detail"]);
$("#name").html(obj.name);*/
/*$.get("results.json", function(data,status){
alert("Status: " + status); alert("Data: " + data);
});*/
/*$.ajax({url: "results.json",
type: "get",
data:{det: JSON.stringify(details)},
contentType: "application/json; charset=utf-8",
dataType: "json",
//success: function(){window.location = "summary.html"}
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}});*/
}
This is my current code, everything is commented out because nothing wants to work. As far as I understand we are not allowed to use PHP