HTML and CSS Reference
In-Depth Information
Listing 7-11. SubmitData() Client-Side Function
function SubmitData(event) {
var data = '';
for (var i = 0; i < storage.length; i++) {
var key = storage.key(i);
var value = storage[key];
var pair = '"' + key + '":"' + value + '"';
data = data + pair + ",";
}
if (data.charAt(data.length - 1) == ',') {
data = data.substring(0, data.length - 1)
}
data = '{' + data + '}';
$.ajax({
type: "POST",
url: "/Home/SaveResults",
contentType: "application/json; charset=utf-8",
data: data,
dataType: "json",
success: function(results){
alert('Results saved!');
window.localStorage.clear();
},
error: function (err) {
alert(err.status + " - " + err.statusText);
}
})
}
The SubmitData() function forms a JSON representation of all the key-value pairs from localStorage
by iterating through the keys. The function sends a JSON dictionary consisting of multiple question-choice
pairs to the server all at once, not one question-choice at a time. The server-side code then needs to
deserialize this JSON dictionary data into a .NET dictionary for further processing. To understand how
SubmitData() sends the data to the server, you need to know what the JSON key-value pairs look like.
Listing 7-12 shows some sample JSON data.
Listing 7-12. JSON Data Being Sent from the Client
{
"FirstName":"Tom",
"LastName":"Jerry",
"Email":" tom@somedomain.com",
"5":"2",
"7":"3",
"1":"1",
"9":"3"
}
As you can see, there are several keys in the JSON data being sent to the server. These keys are
described in Table 7-4.
 
Search WWH ::




Custom Search