rcdmk / aspJSON

A fast classic ASP JSON parser and encoder for easy JSON manipulation to work with the new JavaScript MV* libraries and frameworks.
MIT License
203 stars 89 forks source link

handling post request from axios or vueresource #85

Closed SMen1 closed 5 years ago

SMen1 commented 5 years ago

I am finding aspJSON incredibly useful for generating json from recordsets for get requests. Thank you. It really is superb. I am using Vue2.js applications to consume these.

I am running into problems though trying to use aspJSON to handle posts whereby I might receive an 'application/json' post and then loop through the array.

I apologize if this is extremely basic but I am also 'legacy', and I have been a day and a half on this. I think it might be fundamentally useful though for a solution.

I am receiving an http post to my asp page. The output appears in my console as :

[{\"xyzId\":1,\"abcId\":1564,\"defId\":375 ... }, {\"xyzId\":2,\"abcId\":1564,\"defId\":376 ... }]

I suspect the problem is the escaping \ I am parsing the json I think correctly (see below).

Should this be being handled by aspJSON or should I be doing something else. Code below. I will gladly add to this if you can help me.

%@LANGUAGE="VBSCRIPT"% <% Option Explicit Response.LCID = 2057 ' Brazilian LCID is 1046 (use your locale code here). ' Cold also be the LCID property of the page declaration or Session.LCID to set it to the entire session. %>

<% Response.CharSet = "UTF-8" %> <% Dim JSON set JSON = New JSONobject Dim jsonArr set jsonArr = new jsonArray %> <% If Request.TotalBytes > 0 Then Response.ContentType = "application/json" Dim lngBytesCount lngBytesCount = Request.TotalBytes ' stripped = Replace(BytesToStr(Request.BinaryRead(lngBytesCount)),"\","") set JSONarr = JSON.Parse(BytesToStr(Request.BinaryRead(lngBytesCount))) End If

Function BytesToStr(bytes) Dim Stream Set Stream = Server.CreateObject("Adodb.Stream") Stream.Type = 1 'adTypeBinary Stream.Open Stream.Write bytes Stream.Position = 0 Stream.Type = 2 'adTypeText Stream.Charset = "UTF-8" 'Stream.Charset = "iso-8859-1" BytesToStr = Stream.ReadText Stream.Close Set Stream = Nothing End Function %> <%

JSONarr.Write()

%>

SMen1 commented 5 years ago

sorry [{"xyzId":1,"abcId":1564,"defId":375 ... }, {"xyzId":2,"abcId":1564,"defId":376 ... }] has slashes in the asp code so \ " xyzId \ " or backslash "xyzId backslash ".

Should I be expecting aspJSON to be handling the \ backslash?

rcdmk commented 5 years ago

Hi. Yep. It should handle escaped chars. Please provide some example data to reproduce this error and I can see what is going on.

SMen1 commented 5 years ago

That was such a fast response thank you. It is 23.30 here so I will provide you with as structured an example as I can (my) tomorrow morning. I am getting the issue posting with vueresource and axios, with their default settings (I presume to be application/json). It will be incredibly useful to find a solution.

The JSONarr.Write() gives me the escaped \ " \" characters as per above, as does your loop code that I will follow up with by way of example in the morning.

SMen1 commented 5 years ago

Hi, I found out what the issue was: regarding the escaped characters, this is not aspJSON, but caused by the error response back.

I was getting the error though because I did not easily grasp your loop example.

Particularly this: dim i, item ' more readable loop for each item in JSONarr.items if isObject(item) and typeName(item) = "JSONobject" then item.write() else response.write item end if response.write "
" next

I was able to return all items, but not particular values, and thought that I needed in some way to reference jsonobjects in my post where you use the name "JSONobject". I still do not fully understand why I might use this.

Your solution here though I think could really usefully be a front page example. https://github.com/rcdmk/aspJSON/issues/40

dim i, item for i = 0 to JSONarr.length - 1 variable1 = JSONarr(i).value("jsonkey1") variable2 = JSONarr(i).value("jsonkey2") etc. do something next

This is just so simple and useful now that I have found it!

Thank you for getting back to me so quickly - I think aspJSON is deceptively powerful - but nearly missed the looping.

SMen1 commented 5 years ago

Should anyone find this useful I replaced this from my first post: <% JSONarr.Write() %>

with this to retrieve the values and perform operations on them while looping which is what I wanted to achieve:

dim i, item for i = 0 to JSONarr.length - 1 variable1 = JSONarr(i).value("jsonkey1") variable2 = JSONarr(i).value("jsonkey2") etc. do something next

Works a treat.

rcdmk commented 5 years ago

Glad it works well for you. I'm having a hard time finding spare time to work on it.

Bests.