Writing JSON in Classic ASP page, and general (mis)understanding of Http Response

前提是你 提交于 2019-12-13 16:08:45

问题


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.

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.

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?

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>   

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

sub writeJsonResult(resultXml) 
% > 

        the above javascript is here

< %     end sub
% >

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.


回答1:


AS @Quentin has pointed out;

The service is expecting to get 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"

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

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.


Useful Links

  • Any good libraries for parsing JSON in Classic ASP? [closed]

  • Classic ASP JSON Class (site currently down)

  • JSON object class 3.4.1 by RCDMK




回答2:


The service is expecting to get JSON.

You are giving it an HTML document containing client JavaScript that dynamically writes JSON into the page.

You need to give it actual JSON, so you need to find a way to generate that JSON using ASP.



来源:https://stackoverflow.com/questions/38182963/writing-json-in-classic-asp-page-and-general-misunderstanding-of-http-respons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!