Can I create a REST application using ASP Classic?

If I have ASP Classic, is it possible to build a REST-style application programming interface (API) that accepts input and output data in JSON or even negotiates content and passes the response in JSON or XML or in another format?

As an example, can ASP-Classic be used as support for a jQuery autocomplete widget that sends GET requests and expects JSON responses?

How?

+4
source share
3 answers

Of course, why not?

First, you can program ASP-classic in Javascript. And that means you can take advantage of numerous Javascript extension libraries. In particular, when creating a REST application, you can use json2.js .

This is a REST program that uses ASP-classic, queries the SQLExpress database using a parameterized query and relies on json2.js for encoding, returns a JSON encoded array.

<%@ language="Javascript" %> <script language="javascript" runat="server" src='json2.js'></script> <script language="javascript" runat="server"> if (typeof String.prototype.trim != 'function') { String.prototype.trim = function() { return this.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); }; } (function() { // http://msdn.microsoft.com/en-us/library/windows/desktop/ms675318(v=vs.85).aspx // http://msdn.microsoft.com/en-us/library/windows/desktop/ms678273(v=vs.85).aspx // http://msdn.microsoft.com/en-us/library/windows/desktop/ms675946(v=vs.85).aspx ado = { // data types variant : 12, char : 129, varChar : 200, single : 4, // DBTYPE_R4 date : 133, // DBTYPE_DBDATE time : 134, // DBTYPE_DBTIME // parameter directions input : 1, output : 2, inputOutput : 3, returnVal : 4, directionUnknown : 0, // command types cmdUnspecified : -1, cmdText : 1, cmdTable : 2, cmdStoredProc : 4, cmdUnknown : 8, cmdFile : 256, cmdTableDirect : 512 }; queryDb = function(like) { var rs, result = [], cmd, query, param, conn = new ActiveXObject("ADODB.Connection"); conn.ConnectionString = 'Provider=SQLOLEDB.1;' + 'Integrated Security=SSPI;' + 'Persist Security Info=False;' + 'Initial Catalog=AVWKS2008LT;' + 'Data Source=.\\SQLEXPRESS'; conn.Open(); cmd = new ActiveXObject("ADODB.Command"); cmd.ActiveConnection = conn; cmd.CommandType = ado.cmdText; query = 'SELECT distinct Lastname as lname ' + 'FROM SalesLT.Customer '; if (like !== null) { query += 'WHERE LastName like ? '; like += '%'; param = cmd.CreateParameter("", ado.varChar, ado.input, like.length, like); cmd.Parameters.Append(param); } query += 'ORDER BY lname '; cmd.CommandText = query; rs = cmd.Execute(); // typeof ADODB.Recordset while(!rs.EOF) { // retrieve the 0th field result.push(rs.Fields(0).Value.trim()); rs.MoveNext(); } conn.Close(); return result; }; }()); try { // jquery UI autocomplete requires the search term to be 'term' var t = Request.QueryString('term') + '', token = (t == 'undefined') ? null:t, r = queryDb(token); Response.Write(JSON.stringify(r)); } catch(e) { var error = {error: e.message}; Response.Write(JSON.stringify(error)); } </script> 
+9
source

You can also use something like my aspJSON class: https://github.com/rcdmk/aspJSON

 ' instantiate the class Dim oJSON = New JSON ' add properties oJSON.Add "prop1", "someString" oJSON.Add "prop2", 12.3 oJSON.Add "prop3", Array(1, 2, "three") ' get the JSON formatted output Dim jsonSting jsonString = oJSON.Serialize() ' this will contain the string representation of the JSON object 
+2
source

ASP is just a very underdeveloped Script engine. If you can encode it, he can do it. You can write whatever you want through Response.Write or Response.BinaryWrite . Therefore, the answer should be yes.

If you are going to do JSON, you better work on the server side of JScript. Then you can use standard JSON drivers to generate the JSON result.

0
source

All Articles