Can I build a REST application using ASP Classic?

2020-06-06 01:18发布

If I have ASP Classic, is it possible to build a REST-style application programming interface (API), that accepts input and returns output in JSON, or even does content negotiation and delivers the response in JSON or XML or some other format?

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

How?

3条回答
SAY GOODBYE
2楼-- · 2020-06-06 01:33

Sure, why not?

First, you can program ASP-classic in Javascript. And, that means you can take advantage of the many Javascript extension libraries. In particular , when building a REST app, you might want to use json2.js.

This is a REST program that uses ASP-classic, queries a SQLExpress database using a parameterized query, and relying on json2.js for the 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>
查看更多
成全新的幸福
3楼-- · 2020-06-06 01:45

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
查看更多
家丑人穷心不美
4楼-- · 2020-06-06 01:57

ASP is just very slight souped up Script engine. If you can code it it can do it. You can write whatever you like via Response.Write or Response.BinaryWrite. So the answer has to yes.

If you are gonna do JSON you would be better of working in JScript server-side. You can then using standard JSON stringyfiers to generate JSON result.

查看更多
登录 后发表回答