2

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.

user692942
  • 16,398
  • 7
  • 76
  • 175
Indigo121
  • 75
  • 6

2 Answers2

3

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I understand. Is there no way a caller to this page could get the JSON? Because it is only rendered in the browser? – Indigo121 Jul 04 '16 at 11:07
  • 1
    A caller *could* parse the HTML, execute the JavaScript, then extract the JSON from it. It's certainly *possible*. It's just ridiculous. (And it would require that you edit the software consuming the request). – Quentin Jul 04 '16 at 12:42
  • 1
    @Indigo121 Parse and send back the JSON server-side anything else is just pointless. – user692942 Jul 04 '16 at 12:53
0

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

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175