CODECUBE VENTURES

Ghetto web services

Web services can be confusing/overly complex at times... especially to implement remotely. This is a much easier solution.

This article builds upon the article found at 4guysfromrolla.com titled How to build a low budget web service with classic ASP by Jason Salas

Of course this solution is not as scalable as actual soap web services, but it lends itself well to the sharing of information. Jason goes on to explain in his article that you can write a script to output a javascript file, which can then be refered to remotely by setting the src attribute of the script tag to the remote URL. The browser takes care of loading the remote file and executing any javascript statements.

However, If you want a drastically more powerful solution using the same methodology, there is a better way...

All server side languages provide a way to change the MIME type of the response to the browser. This is text/html by default, but it can be easily changed to any valid MIME type that a browser would understand. Of course you've got to have the data to back it up... for example, it's prefectly plausible to have a MIME type of image/jpg, and output the contents of a picture using the filesystemobject (of course there are many methods to do such a task, but I'll leave that for another day ;).

The MIME type for javascript is text/javascript. As the name suggests, this is a text output, so you could have an ASP file that looks like this.

<% Response.ContentType = "text/javascript" %>

function writeit() {
document.write("this is an ASP javascript file!!");
}

The above file can then be referenced as follows

<script language="javascript" src="http://www.yourdomain.com/jsfile.asp"></script>

Since the output is simply text, you can then use your favorite server side language to customize that output based on any number of sources

  • Database content

  • File information

  • etc...

Of course, as any file, you can always include querystring parameters to further the ability to provide dynamic content. below is an actual script that can be found at http://www.codecube.net/demos/js.aspx

<%@ page language="C#" debug="true" %>
<% Response.ContentType = "text/javascript" %>
<%
Response.ContentType = "text/javascript";
Response.Write("document.write('<h1>hello " + Request.QueryString["name"]
+ "</h1>');");
for (int i=0;i<10;i++) {
Response.Write("document.write('"+ i +" is remote baby!!<br>');");
}
%>

Notice how it is looking for a querystring parameter called name... all you have to do is refer to this file like this

<script language="javascript" src="http://www.codecube.net/
demos/js.aspx?name=codecubereader"></script>

A demo of this can be seen here. Of course, this also works over different domain names.

I've included the syntax for several of the popular server side languages below to change the mime type

  • PHP

Header("Content-type: text/javascript");

  • Cold Fusion
  • ASP/ASP.net

Response.ContentType="text/javascript"

  • JSP

response.setContentType("text/javascript")

Latest post: Digging Up the First Version of CodeCube

See more in the archives