Reputation: 1467
Basically which would be the simplest way to get data from a database and feed it into a javascript array without the user being able to see the data?
I'm making a simple hangman game web app as a college project and I basically have to have the game's list of words in a database, which I've done. I also know how to connect and how put database data into a data source or how to read the data manually using a reader.
What I want to know is how to feed the data (not the whole database but just the data that I read in) into javascript. For example let's say I've got 1000 values in the database and I've extracted 15 different values from the database in the ASP.NET PageLoad event, now what do I do to get these 15 values into JS without enabling the user to see these vales by checking the page source?
Upvotes: 0
Views: 2656
Reputation: 6948
From your question, it sounds like you are working with ASP.NET web forms. If that is the case, you will probably want to retrieve your 15 values using an AJAX call. If you build the JS synchronously in the Page_Load, it will be easy to view source and see what is going on... That said, anybody can turn on Network capture and see the hangman values coming back in the AJAX response.
Maybe consider using a page method. You would make a static web method in your code behind. Here is what pseudo code would look like with C# and jQuery.
On the server in your code behind (say it is hangman.aspx.cs)
[WebMethod]
[ScriptMethod]
public static string[] GetHangmanValues()
{
string[] values = new string[15];
//retrieve your hangman values from the db and return them
return values;
}
On the client side:
var HANGMAN_APP = {};
$(function () {
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "hangman.aspx/GetHangmanValues",
data: "{}",
success: function (data) {
//Microsoft page methods wrap the data in data.d
//use data.d to populate HANGMAN_APP
},
error: function () {
alert("Error calling the page method.");
}
});
});
This way of doing it will keep the values from being displayed in page source, but I just want to reiterate: Network traffic and/or a javascript debugger would not protect your values if somebody really wanted to see them. Also, an extra benefit is that Page Methods don't go through the entire page life cycle or deal with ViewState (like UpdatePanels do), which makes them very efficient for this sort of thing.
Upvotes: 2