問(wèn)題描述
我有一個(gè)經(jīng)典的 ASP 頁(yè)面 (VBscript),它在服務(wù)器端生成一個(gè) XML,然后 Response.Writes 它.該頁(yè)面根本沒(méi)有客戶(hù)端.
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.
但是我需要將其轉(zhuǎn)換為 JSON.由于我找不到有效的 ASP 服務(wù)器端方式(完全不同的主題),所以我在客戶(hù)端使用我找到的 Javascript 代碼完成了它,最后將它記錄到頁(yè)面中.
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.
問(wèn)題是結(jié)果不一樣:如果在 http RESPONSE 之前只是一個(gè) XML,那么現(xiàn)在的響應(yīng)是 javascript 代碼,它將 JSON 寫(xiě)入瀏覽器,而不是響應(yīng).我理解對(duì)了嗎?
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?
換句話(huà)說(shuō),如果之前我有一個(gè) xml 作為響應(yīng),那么現(xiàn)在的響應(yīng)是這樣的:
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>
整個(gè)塊由 ASP 在這樣的方法中調(diào)用:
This whole block is called by the ASP inside a method like this:
sub writeJsonResult(resultXml)
% >
the above javascript is here
< % end sub
% >
同樣,瀏覽器明顯顯示 JSON,但使用它的服務(wù)沒(méi)有獲得所需的響應(yīng).有沒(méi)有辦法將 JSON 寫(xiě)為響應(yīng)?我覺(jué)得我遺漏了一些東西并且不太理解這一點(diǎn).
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 有 指出;
服務(wù)期望獲取 JSON.
這意味著試圖通過(guò)處理 JSON 客戶(hù)端來(lái)解決這個(gè)問(wèn)題是行不通的,因?yàn)檫@意味著您已經(jīng)發(fā)回了 text/html
HTTP 響應(yīng)而不是 application/json
一.無(wú)法繞過(guò)它,您必須處理 XML 以在服務(wù)器端構(gòu)建 JSON 結(jié)構(gòu),然后使用返回它
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 庫(kù),有的很好,有的很棒,有的簡(jiǎn)直太糟糕了.如果您正在尋找推薦 ASPJSON.com 可能是使用最廣泛的庫(kù)(但奇怪的是,該網(wǎng)站目前似乎已關(guān)閉).
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).
如果可能的話(huà),在生成 XML 的地方使用上述庫(kù)將其替換為 JSON,它們中的大多數(shù)都支持直接從數(shù)據(jù)庫(kù)構(gòu)建 JSON 結(jié)構(gòu),從而節(jié)省您自己從 XML 解析和構(gòu)建 JSON 的時(shí)間.
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.
在經(jīng)典 ASP 中解析 JSON 有什么好的庫(kù)嗎?[關(guān)閉]
經(jīng)典 ASP JSON 類(lèi) (站點(diǎn)當(dāng)前關(guān)閉)
RCDMK 的 JSON 對(duì)象類(lèi) 3.4.1
這篇關(guān)于在經(jīng)典 ASP 頁(yè)面中編寫(xiě) JSON,以及對(duì) Http 響應(yīng)的一般(錯(cuò)誤)理解的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!