views:

81

answers:

4

Hi,

I am thinking of this idea: 1. Request data from server and write to javascript file in json format using C# 2. When the page loads, the page loads the js file and I can use the data using javascript?

is this a good or bad idea? is there any example somewhere doing that?

thanks

+1  A: 

I'd take into account:

  • Performance, do you expect the dataset to be small enough to be managed by browsers? How complex are the relationships between your entities?
  • Security, how will you secure the data to avoid malicious code execution?

Taking into account these concerns I'd recommend JSON for data interfaces.

Anero
it is only one table with 7 fields, as well as it will not contains that much data.only authorized person can load the json, and it is read only.
user1111111
If he outputs JSON, he should be fine security wise - as long as he makes sure user-data ends up properly escaped in strings, which, unless he's reinventing the JSON outputter wheel, should be done by whatever JSON library he's using.
Thanatos
A: 

JavaScript performance varies by browser, and can go downhill under load, but with that small of a dataset you shouldn't have to worry about that.

If you are using MVC, AJAX and JSON are very easy.

ThatSteveGuy
A: 

My company does this occasionally when the page is meant to hold only static data. It looks something like this in a .aspx document:

var initialData = <asp:Literal runat="server" id="MyAwesomeLiteral" Text="null" />;

myObject.init(initialData);

This only works if you're confident that your data can't be manipulated to bad ends. If you have IDs, especially sequential ones, encrypt them.

spamguy
A: 

There are better ways. Rather than write to a file, you could create a script tag that points to a server page that generates the script like this:

<script type="text/javascript" src="GenerateScript.ashx"></script>
<script type="text/javascript">
// write some page level code here
</script>

Make sure you test in a few browsers because some may cache the dynamic script. You can also pass parameters to your handler:

<script type="text/javascript" src="GenerateScript.ashx?x=1&y=2"></script>
<script type="text/javascript">
// write some page level code here
</script>
dana