Reputation: 9869
if someone could tell me what is wrong with this rather simple code inside my file called "test.asp"?
<script src="jquery-1.2.6.js" type="text/javascript" language="javascript" runat="server"></script>
<script src="jquery.xml2json.js" type="text/javascript" language="javascript" runat="server"></script>
<%
var xml = '<xml><message>Hello world</message></xml>';
var json = $.xml2json(xml);
alert(json.message);
%>
The error message that I am getting is Microsoft JScript runtime error
'$' is undefined
I have tried XMLObjectifier as well as xml2json.js, and the common theme is that I can't seem to execute these javascript libraries inside my classic ASP file.
My understanding is that JScript, which is what ASP is written in, is javascript...just on the server side. So can I run/reference .js files inside my test.asp file?
Thank you very much! As you can tell, my asp file produces xml, but I want to transform it to json.
Upvotes: 1
Views: 5111
Reputation: 17966
I've used JScript to interop between a modern React (node) project, a legacy classic ASP project and an aging web forms project. Classic ASP's ability to use JScript on the server was extremely useful. I was able to centralize all settings (api, connection strings, app settings, etc.) between Classic ASP, web forms, and a node (react) project. Once they were all reading the same settings, I removed their individual web.config settings
Babel has a JScript transform that lets you transpile your code an additional level to JScript (I transpiled ES2016 -> ES5 -> JScript for the Classic ASP bundle).
include/config.js (babel-generated settings)
var config = {
"cdnBaseUrl": "https://api.example.com",
"apis": { ... },
...
}
include/Config.asp
' Import the global JSON object from crockford to parse JSON in VBScript server-side
<script language="JScript" runat="server" src="json2.js"></script>
' Import global config object that can be called from JScript and VBScript interchangeably
<script language="JScript" runat="server" src="config.js"></script>
<script language="JScript" runat="server">
/** DEFINE GLOBAL FUNCTIONS THAT CAN INTEROP WITH VBSCRIPT HERE */
function requireScript(path) {
var resourceUrl = config.cdnBaseUrl + path
return ('<sc' + 'ript src="' + resourceUrl + '"></sc' + 'ript>')
}
....
</script>
include/AppHead.asp
<!-- Include the settings and global helpers -->
<!--#include virtual="/include/Config.asp"-->
<!-- Build script tags in head that point to the correct CDN base url -->
<%=requireScript("/assets/react-client.js")%>
It would have been torture to write adapters for all the settings (parsing XML / JSON with VBScript is terrible).
Upvotes: 0
Reputation: 42363
Please note - I am not ignoring the fact that JScript can be used in the browser as per the excellent info on this answer: What's the difference between JavaScript and JScript?. This question clearly is talking about JScript in the context of the server, so from here on the term 'JScript' means the version(s) of JScript that are used in ASP or in Windows scripting
Two languages can be equal/similar (although please note JScript is not exactly JavaScript), but their runtime environments can be completely different.
The JavaScript that these libraries you're trying to use is a client-side scripting language that expects to run in a browser. The browser is then the runtime environment for this code, providing the objects and services as laid out in the HTML spec (for example, the intrinsic window
or document
objects).
JScript is a javascript-like language that, in this case, expects to run on a server (or in the Windows scripting environment) - i.e. in a completely different runtime environment to a browser
In this context, then, the idea of including a client javascript library on the server is, well, erroneous to put it politely. The closest you can ever really get is on a server platform like Node.js which is 'pure' JavaScript; but even that can't run client libraries like jQuery because they rely on the runtime environment provided by the browser. Yes, that can be 'faked' and 'stubbed'; but not for any real benefit.
Stop trying to do this, and rewrite it as normal client-side code.
Upvotes: 3