0

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

Charl Meyers
  • 115
  • 1
  • 8
  • 1
    You can leave out the `var jqxhr=` but what does the console say and what does the summary page do with the passed data? The excercise sounds silly to me. – mplungjan May 24 '15 at 15:47
  • I know it is stupid, literally nothing happens – Charl Meyers May 24 '15 at 15:54
  • Console errors? Your commented getResults is certainly partly php. There is no point in passing a string to a html page that gets loaded using ajax – mplungjan May 24 '15 at 15:56
  • Oh yes I commented that out once I realised that $_GET is php, I kinda forgot and tried to use that and only realised later on that you cannot use $_GET in javascipt – Charl Meyers May 24 '15 at 16:05
  • No you use location.search but that is not available to the page when ajaxed – mplungjan May 24 '15 at 17:38

1 Answers1

0

I don't have the answer per se but data between html pages might give you the insight you require

After careful thought and quick experiment, this post will point you in the correct direction passing data from an html page to another in javascript

As one of the comments state that the Ajax is not for passing data between pages it's for client server async communication. I would suggest in future use the local session rather than posting or getting data for inter page use.

Community
  • 1
  • 1
  • The most I have been able to do was make the url summary.html in the ajax method. This is my summary page. In my console error I can see that it actually adds parameters to that url, but then how do I access and navigate to that url? – Charl Meyers May 25 '15 at 05:43