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.
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
windowordocumentobjects).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.