问题
I have some logic I want to run on the server-side. It's implemented in Javascript, and I'd like to use it to generate and emit JSON, to allow a REST-api for a web app I'm producing.
Development is on Windows7 and IIS. I know IIS still supports ASP, which can be implemented in Javascript.
Is it possible for an ASP classic page to emit JSON?
回答1:
Yes, no problem. It's possible to use the well-known json2.js from json.org within a Javascript-based "classic ASP" page.
Per ejemplo:
<%@ language="Javascript" %>
<script language="Javascript" runat="server" src='json2.js'></script>
<script language="Javascript" runat="server">
(function() {
scriptEngineInfo = function () {
var s = {
engine : ScriptEngine(),
version: {
major: ScriptEngineMajorVersion(),
minor:ScriptEngineMinorVersion()
},
build: ScriptEngineBuildVersion()
};
return s;
}
}());
var x = scriptEngineInfo();
var d = new Date();
x.Timestamp = d.valueOf();
Response.Write (JSON.stringify(x));
</script>
回答2:
This is a basic example of how to create a .json
file with classic ASP
.
<%
Response.ContentType = "application/json"
Response.Write("{ ""responseCode"": ""success"", ""accountNumber"": ""78527511"", ""ID_Code"": ""654321"", ""version"": ""1""}")
%>
having as a result:
{
"responseCode": "success",
"accountNumber": "78527511",
"ID_Code": "654321",
"version": "1"
}
回答3:
Here is a great article on it which includes some example code: http://www.webdevbros.net/2007/04/26/generate-json-from-asp-datatypes/
Its best to put you data in a properly structured array and this code will show you have to take an array and output JSON formatted text.
来源:https://stackoverflow.com/questions/9746000/can-i-generate-json-from-classic-asp-on-iis