How to use JSON (.parse() / .stringify() ) in IIS server-side

3.8k views Asked by At

Virtually all of my server access is via AJAX... my server side files open a connection to my sql-server database and either write to or read from the current table(s).

I am saving my data in my sql-server table as JSON strings. For example:

[ {"score":"game","bet":"20"},
  {"score":0,"bet":"10"},
  {"score":1,"bet":"11"},
  {"score":2,"bet":"12"},
  {"score":3,"bet":"13"},
  {"score":4,"bet":"14"},
  {"score":5,"bet":"15"},
  {"score":6,"bet":"16"},
  {"score":7,"bet":"17"},
  {"score":8,"bet":"18"},
  {"score":9,"bet":"19"}
]

I have found that I need to do some validation and verification on the server. Ideally I'd like to pull this string out of the table, convert it to JSON (via JSON.Parse() I presume) perform the validation/verification and then make the response back to the client depending on the result.

I have looked at Node.js but I am not in a place to move my entire server from IIS to Node.js. Ideally I'd like to be able to just process javascript inside my standard ASP pages.

* Friday 12/13/2013 ***********

Here is most of the source code from the file being called by AJAX:

<%@LANGUAGE="JAVASCRIPT"%>

<script runat="server" src="scripts/json2.js"></script>

<%

var JSONstr;
var GridId;
var password;
var where;

var adoConn;
var adoComm;
var adoRS;

var json;

adoConn = Server.CreateObject("ADODB.Connection");
adoComm = Server.CreateObject("ADODB.Command");
adoRS = Server.CreateObject("ADODB.Recordset");

adoConn.ConnectionString = footballConnStr;
adoConn.Open();

GridId=Request.Form('GridId');

where = "Id = '" +GridId +"'"
password=Request.QueryString('password');

adoRS.ActiveConnection = adoConn;
adoRS.CursorType = 3; // So I can use RecordCount Property

adoRS.Source="SELECT GridBets FROM _Games WHERE " +where

// ** this is my little test to see if the JSON object from the JSON2.js is getting loaded

json = {};
JSONstr = JSON.stringify(json); // it is failing on this line...

// **

adoRS.Open();
    var rslt = "{ digits:" +adoRS('GridBets') +"}";
adoRS.Close();

Response.Write( rslt );
Response.End();

/*

2

There are 2 answers

6
brandonscript On

Your best bet is to use a JSON library for ASP. You haven't indicated whether or not you're using classic ASP though?

Some options for ASP.NET:

There are also a plethora of similar questions already on SO:

0
foxontherock On

You were near to a solution. json2.js CAN work with asp classic perfectly, using VB or Javascript.

But, when you use the <%@ language="javascript" %> initialiser, all the code between <% %> is executed BEFORE your server-side javascript include.

So, the running solution can be to put your javascript code in an external JS file, or set it in asp file between script runat="server" instead of <% %>.

Sample json.asp:

<%@ language="javascript" %> 
<script language="javascript" runat="server" src="json2.js"></script>
<script language="javascript" runat="server">
    var s = JSON.stringify({aaa: "bbb"});
    Response.Write(s + '<br>');
    var obj = JSON.parse('{"ccc": "ddd"}');
    Response.write(obj.ccc + '<br>');
</script>
<script runat="server" language="vbscript">
    response.write "some vbscript code<br>"
</script>
<% 
    try{        
        var obj = {a: "a", b: "b"};
        Response.Write(JSON.stringify(obj));
    }catch(e){
        Response.Write("ERR: " + e.message + "<br>")
    }
%>

The output is:

some vbscript code
ERR: 'JSON' is undefined
{"aaa":"bbb"}
ddd

As you can see, we get the "ERR:" output BEFORE the {"a": "b"}, because code between <% %> is executed before running any server-side javascript between script tags. But, you see that vbscript code between script tags is executed before anything else, even if defined after!