Get Classic ASP variable from posted JSON

后端 未结 8 1985
挽巷
挽巷 2021-02-08 11:01

I\'m trying to post JSON via AJAX to a Classic ASP page, which retrieves the value, checks a database and returns JSON to the original page.

I can post JSON via AJAX. I

相关标签:
8条回答
  • 2021-02-08 11:14

    Generally (new VBArray(arr).toArray()) should work on a SafeArray, which is what Request.BinaryRead returns. But it doesn't. I found this solution which I revised:

    function GetRawRequestData() {
        var byteCount = Request.TotalBytes;
        var binary = Request.BinaryRead(byteCount)
        var reader = Server.CreateObject('ADODB.Recordset');
        reader.Fields.Append('x', 201, byteCount);
        reader.Open();
        reader.AddNew();
        reader.Fields(0).AppendChunk(binary);
        reader.update();
        return reader.Fields(0).Value;
    }
    

    thanks to Rasberry's comment @ http://blogs.msdn.com/b/david.wang/archive/2006/07/04/howto-convert-between-jscript-array-and-vb-safe-array.aspx

    Note that it might not handle encoding correctly. I didn't dive into what 201 for ADO datatype means, but it works.

    0 讨论(0)
  • 2021-02-08 11:19

    You may consider switching from VBScript to JScript (JScript is what microsoft calls JavaScript).

    Why? Because then from within Classic ASP you can do the same JSON calls that the browser can do and read the results into JavaScript objects using the eval() statement.

    Paddy Said: I don't have an answer, but you have my every sympathy... classic asp and JSON handling - sounds like fun.

    @Paddy: Classic ASP and JSON IS fun, in fact it ROCKS! (If you switch from VBScript and use JScript.)

    Note that you don't have to quit VBScript cold-turkey, you can still interoperate between the two in the same ASP file but if you declare JScript first you need to confine your VBScript to SUB or functions and vice-versa otherwise unpredictable things can happen.

    Here's a quick example of what I'm talking about:

    <%@ LANGUAGE="JScript" %>
    <%
    
    var days = VBDateDiff("d", "4/10/2010", "5/3/2010");
    Response.write("JScript Calling VBScript function: days = " + days);
    
    
    %> <script language="VBScript" runat="server">
    function VBDateDiff(units, datebefore, dateafter)
        VBDateDiff = CStr(DateDiff(units, datebefore, dateafter))
    end function
    
    function VBDateAdd(units, nUnits, theDate)
        Response.write("<BR>VBDateAdd units=" & units & ", nUnits=" & nUnits & ", theDate=" & theDate)
        VBDateAdd = CStr(DateAdd(units, nUnits, theDate))
        Response.write(", VBDateAdd=" & VBDateAdd)
    end function
    </script> <%        
    
    %>  
    
    0 讨论(0)
  • 2021-02-08 11:20

    Here is the solution that i used in ASP Vbscript with Radium's code and some corrections:

    bytecount = Request.TotalBytes
    bytes = Request.BinaryRead(bytecount)
    
    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"                      
            s = stream.ReadText() 'here is your json as a string                
        stream.Close()
    Set stream = nothing
    
    Response.write(s)
    
    0 讨论(0)
  • 2021-02-08 11:20

    I having same issue but very soon i figure out how to post a json object to ASP server.

    Here is the idea: not tested. Work hard on it ><.

    Parse the json object into string then post back to server.(this is a typical string posting)

    on ASP side, use JSON library to parse the string back into the object.

    link for the JSON library http://code.google.com/p/aspjson/

    'I assume ppl that try post JSON back to asp should have basic posting knowledge. Any further assistance please let me know

    0 讨论(0)
  • 2021-02-08 11:27

    alphadogg's code worked for me, but only after I specified a little more information:

    bytecount = Request.TotalBytes
    bytes = Request.BinaryRead(bytecount)
    
    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";                        
    Set s = stream.ReadText();                       
    stream.Close();                                  
    

    Prior to this I would get "Operation is not allowed in this context." as jjokin reported.

    0 讨论(0)
  • 2021-02-08 11:32

    Don't know if you are still looking, but this answer may help you. However, the code is for ASP.NET pages. The classic ASP analog is:

      byteCount = Request.TotalBytes
      requestBody = Request.BinaryRead(byteCount)
    

    Then, you probably want to turn it into a string to parse/use. You can use ADODB.Stream for the conversion.

      Set bStream= CreateObject("ADODB.Stream")
      bStream.Open
      bStream.Write requestBody
      bStream.Position = 0
      bStream.Type = adTypeText 
      str = bStream.ReadText
    
    0 讨论(0)
提交回复
热议问题