Tuesday, 14 February 2012

Classic ASP and JSON

Yes, sure having to deal with some classic ASP code in 2012 does not seem like the most exciting programming task that you can come across... but there are still tons of classic asp sites running out there (for example easyjet.com used to have some visible classic asp pages till rather recently), so, if for some reason you need to do some addition to a classic ASP site, instead of changing your mindset to the programming paradigm of the late 90's with which the site was originally developed, why not go the other way around and try to apply a modern paradigm to to that old technology

The other day I had to send the data in a "complex" form (well, it had different sections that could be dynamically added) to an asp page. I've used Ajax to call into a classic ASP backend many times before, but in this occasion the type of data to send didn't lend itself too well to be formatted as the classic "&=" chain (I'm not sure what's the proper name for this, form encoded?) but should be sent as JSON. So, the thing is, how do I read the JSON data on the classic ASP side?

  • First, you'll need something to savely parse the incoming JSON (needless to say that just applying eval to a user supplied string is a recipe for disaster). Probably you've already used json2.js on your client code, so take advantage of the fact that classic ASP was one of the first Server Side JavaScript frameworks in the market and use it on the Server too.
  • Second, you'll have to read the JSON string to pass it over to JSON.parse(). This is the part that proved to be more tricky.
    First I thought I could get the JSON string by accessing the Request.Body property (after all you're trying to get access to the Body of the Http Request), but this didn't work, in fact I didn't realize it was that what was failing until I threw the Visual Studio debugger into the corresponding dllhost.exe process. In Asp.Net you have access to this using the Request.InputStream, but the only similar thing to this in classic ASP seems to be using the BinaryReader method, which appears to me like too much hassle. The work around I used was sending from the client a normal Form key-value chain, with just a key (data for example) and the JSON string as value. That is, in the end I had to change my initial code from this:
    //client side:
    $.ajax({
        async: true,
        url: .........
        type: "POST",
        dataType: "text",
        contentType: "application/json, charset=utf/8",
        data: JSON.stringify(self.formToObject()),
        ....
    
    //server side:
    JSON.parse(Request.Body);
    

    to this:

    //client side:
    $.ajax({
        async: true,
        url: .........
        type: "POST",
        dataType: "text",
        contentType: "application/x-www-form-urlencoded",
        data: "data=" + JSON.stringify(self.formToObject()),
    ...
    
    //server side:
    JSON.parse(Request.Form("data"));
    

2 comments:

  1. Hi,

    where would you put the client side code? What libraries are needed for this?
    I am having a similar situation. I have a url that accepts json from postmarkapp.com. I manage to handle the json but I can't actually get it from the service.

    Any help is appreciated.

    Thank you,
    Dimitris

    ReplyDelete
  2. Hi Dimitri,

    I'm using jQuery (for the $.ajax thing) and json2.js (for the JSON.stringify thing):
    https://github.com/douglascrockford/JSON-js

    I'm doing the ajax call when the user clicks on a send button:
    $("#myButtonId").click(function(){
    $.ajax(...
    });

    Hope it helps.

    ReplyDelete