本文介绍了在经典 ASP 页面中编写 JSON,以及对 Http 响应的一般(错误)理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个经典的 ASP 页面 (VBscript),它在服务器端生成一个 XML,然后 Response.Writes 它.该页面根本没有客户端.

I had a classic ASP page (VBscript) that generates an XML on server-side, and then Response.Writes it. The page had no client side at all.

但是我需要将其转换为 JSON.由于我找不到有效的 ASP 服务器端方式(完全不同的主题),所以我在客户端使用我找到的 Javascript 代码完成了它,最后将它记录到页面中.

However I needed to convert it to JSON. Since I could not find an ASP server-side way that worked (whole different subject) I did it on client-side with a Javascript code I found, finally document.writing it to the page.

问题是结果不一样:如果在 http RESPONSE 之前只是一个 XML,那么现在的响应是 javascript 代码,它将 JSON 写入浏览器,而不是响应.我理解对了吗?

THE PROBLEM is that the result is not the same: If before the http RESPONSE was only an XML, the response now is the javascript code, which writes to the browser the JSON , but not to the response. Am I understanding this right?

换句话说,如果之前我有一个 xml 作为响应,那么现在的响应是这样的:

In other words, if before I had an xml as the response, now the response is this:

    <script type="text/javascript">
        var xmlObj = parseXml('<%=resultXml%>');
        var json = xml2json(xmlObj);
        document.write(json);
    </script>

整个块由 ASP 在这样的方法中调用:

This whole block is called by the ASP inside a method like this:

sub writeJsonResult(resultXml)
% >

        the above javascript is here

< %     end sub
% >

同样,浏览器明显显示 JSON,但使用它的服务没有获得所需的响应.有没有办法将 JSON 写为响应?我觉得我遗漏了一些东西并且不太理解这一点.

So again, visibly the browser shows the JSON, but the service that uses it doesn't get the RESPONSE it needs. Is there a way to write the JSON as response? I feel I am missing something and not quite understanding this.

推荐答案

AS @Quentin指出;

服务期望获取 JSON.

这意味着试图通过处理 JSON 客户端来解决这个问题是行不通的,因为这意味着您已经发回了 text/html HTTP 响应而不是 application/json 一.无法绕过它,您必须处理 XML 以在服务器端构建 JSON 结构,然后使用返回它

This means trying to get around that by processing the JSON client-side isn't going to work as that would mean you have sent back a text/html HTTP response instead of a application/json one.There is no getting around it you have to process the XML to build a JSON structure server-side then return it using

Response.ContentType = "application/json"

有很多适用于 Classic ASP 的 JSON 库,有的很好,有的很棒,有的简直太糟糕了.如果您正在寻找推荐 ASPJSON.com 可能是使用最广泛的库(但奇怪的是,该网站目前似乎已关闭).

There are lots of JSON libraries out there for Classic ASP, some good, some great and some just plain awful. You just need to have a look through and see which one suits you, if you are looking for a recommendation ASPJSON.com is probably the most widely used library (but weirdly the site appears to be down at the moment).

如果可能的话,在生成 XML 的地方使用上述库将其替换为 JSON,它们中的大多数都支持直接从数据库构建 JSON 结构,从而节省您自己从 XML 解析和构建 JSON 的时间.

If possible where the XML is generated replace this with a JSON using a library like described above, most of them support building JSON structures directly from the database, saving you on parsing and building the JSON from the XML yourself.

经典 ASP JSON 类 (站点当前关闭)

RCDMK 的 JSON 对象类 3.4.1

这篇关于在经典 ASP 页面中编写 JSON,以及对 Http 响应的一般(错误)理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 08:48