Having a problem while creating a server-side REST API using Classic ASP/

Viewed 184

While there's not much available detailing what happens on the server side of a Rest API written in VBScript, there is one article that addresses this issue: Can I build a REST application using ASP Classic?. The bulk of that post describes various issues regarding JSON stringifying as well as acquiring data from a database. My question concerns neither of these. But ignoring these (and a few syntax errors), there is very little else in that post.

So, I admit to being very much of a novice regarding server-side behaviour. But when I strip away everything from the above-mentioned post, I am left with a quite bare few lines of code that appear to simply request an input value and then output a string. So I wondered if this is indeed all that is involved in the bare-bones I/O (i.e., ignoring the security and formatting issues). I decided to try this out. My results are puzzling. When I call this incredibly simple API, instead of receiving the very simple JSON string that is being sent, I instead get back the entire piece of source code comprising the API, from the opening HTML tag to its closure. I've obviously made a very fundamental error.

Here is my code. First, here is the extremely bare-bones REST API itself (please note: this is ONLY writing back an artificial JSON string. It's not even concerning itself with receiving the POST parameters.) This is "simplerest.asp"

<html>
<head>
</head>
<body>
<%
Response.Write("{" & Chr(34) & "SomeCert" & Chr(34) & ":" & Chr(34) & "12345"& Chr(34) & "}")
%>
</body>
</html>

And here is the code (utilizing jQuery) to call this "API" (testrest.asp):

<html>
<head>
<script type="text/javascript" src="/./include/jquery-3.4.1.min.js"></script>
</head>
<body>
    <script type="text/javascript">
        function getCert(certNumber) 
        {
            var settings = {
                "async": true,
                "crossDomain": true,
                "url": "example.com/simplerest.asp",
                "method": "GET",
                "success": function (response) {
                    alert("success");
                    },
                "headers": {
                      "Accept": "application/json"
                }
            }
            $.ajax(settings).done(function (response) {
                certvals = response;
                alert("certvals is " + certvals);
            });
        }
        getCert("dummy");
    </script>
</body>
</html>

As I stated above, when I run the calling logic, what is returned in certvals is the precise ASCII representation of the entire "API" code. In the spirit of what I am attempting, which is to see an API running in its most elementary mode, can someone enlighten me as to what dumb error I am making?

1 Answers

Assuming you have Classic ASP installed and enabled in your website (IIS), use;

<%
'Tell client you are sending JSON
Response.ContentType = "application/json"
Call Response.Write("{""SomeCert"":""12345""}")
%>

Because you had the JSON encapsulated in a HTML structure it was just being output as a string in the HTML page.

Also, there is no need for Chr(34) when you can escape double quotes in strings by doubling them.


In regard to the comments, here is a fully working example tested using a local instance of IIS.

<html>
  <head>
    <title></title>
  </head>
  <body>
    <button id="buttonSend">Send</button>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script>
      $("#buttonSend").on("click", function() {
        getCert("dummy");
      });

      function getCert(certNumber) 
      {
        var settings = {
          "async": true,
          "crossDomain": true,
          "url": "example.com/simplerest.asp",
          "method": "GET",
          "success": function (response) {
            alert("success");
          }
        }
        $.ajax(settings).done(function (response) {
          certvals = response;
          alert("certvals is " + certvals.SomeCert);
        });
      }
    </script>
  </body>
</html>

Outputs (two messages):

success
certval is 12345

Useful Links

Related