jQuery 从发布的 JSON 中获取经典 ASP 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2682280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:02:48  来源:igfitidea点击:

Get Classic ASP variable from posted JSON

jqueryjsonasp-classic

提问by Will

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.

我正在尝试通过 AJAX 将 JSON 发布到经典 ASP 页面,该页面检索值、检查数据库并将 JSON 返回到原始页面。

I can post JSON via AJAX. I can return JSON from ASP. I can't retrieve the posted JSON into an ASP variable.

我可以通过 AJAX 发布 JSON。我可以从 ASP 返回 JSON。我无法将发布的 JSON 检索到 ASP 变量中。

POST you use Request.Form, GET you use Request.Querystring. What do I use for JSON?

POST 使用 Request.Form,GET 使用 Request.Querystring。我对 JSON 有什么用?

I have JSON libraries but they only show creating a string in the ASP script and then parsing that. I need to parse JSON from when being passed an external variable.

我有 JSON 库,但它们只显示在 ASP 脚本中创建一个字符串然后解析它。我需要在传递外部变量时解析 JSON。

Javascript

Javascript

var thing = $(this).val();

$.ajax({
         type: "POST",
         url: '/ajax/check_username.asp',
         data: "{'userName':'" + thing + "'}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         cache: false,
         async: false,
         success: function() {
            alert('success');
         }
});

ASP file (check_username.asp)

ASP 文件 (check_username.asp)

    Response.ContentType = "application/json"
          sEmail = request.form() -- THE PROBLEM

          Set oRS = Server.CreateObject("ADODB.Recordset")
          SQL = "SELECT SYSUserID FROM dbo.t_SYS_User WHERE Username='"&sEmail&"'" 
          oRS.Open SQL, oConn
          if not oRS.EOF then 
            sStatus = (new JSON).toJSON("username", true, false)
          else
            sStatus = (new JSON).toJSON("username", false, false)
        end if
response.write sStatus

回答by Laurence Dougal Myers

alphadogg's solution didn't work for me, I got errors with the line bStream.Write requestBody(saying "Operation is not allowed in this context.") This seems to work for me, and returns the whole request string. However, it will only work for request data <= 100 KB, otherwise you'll have to work out how to get the BinaryRead method working.

alphadogg 的解决方案对我不起作用,我遇到了该行错误bStream.Write requestBody(说“在此上下文中不允许操作。”)这似乎对我有用,并返回整个请求字符串。但是,它仅适用于 <= 100 KB 的请求数据,否则您将必须弄清楚如何使 BinaryRead 方法工作。

str = Request.Form

(Discovered from http://msdn.microsoft.com/en-us/library/ms525985%28v=VS.90%29.aspx)

(从http://msdn.microsoft.com/en-us/library/ms525985%28v=VS.90%29.aspx发现)

回答by kenchilada

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

alphadogg 的代码对我有用,但只有在我指定了更多信息之后:

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.

在此之前,我会得到“在这种情况下不允许操作”。正如 jjokin 报道的那样。

回答by CarlosKaval

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

这是我在带有 Radium 代码和一些更正的 ASP Vbscript 中使用的解决方案:

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)

回答by alphadogg

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

不知道你是否还在寻找,但这个答案可能对你有帮助。但是,该代码适用于 ASP.NET 页。经典的 ASP 模拟是:

  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.

然后,您可能希望将其转换为要解析/使用的字符串。您可以使用 ADODB.Stream 进行转换。

  Set bStream= CreateObject("ADODB.Stream")
  bStream.Open
  bStream.Write requestBody
  bStream.Position = 0
  bStream.Type = adTypeText 
  str = bStream.ReadText

回答by Marlin

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

您可以考虑从 VBScript 切换到 JScript(JScript 是微软所说的 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.

为什么?因为在 Classic ASP 中,您可以执行浏览器可以执行的相同 JSON 调用,并使用 eval() 语句将结果读入 JavaScript 对象。

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

Paddy Said:我没有答案,但你有我的全部同情...经典的 asp 和 JSON 处理 - 听起来很有趣。

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

@Paddy:经典的 ASP 和 JSON 很有趣,事实上它很有趣!(如果您从 VBScript 切换并使用 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.

请注意,您不必退出 VBScript 冷火鸡,您仍然可以在同一个 ASP 文件中在两者之间进行互操作,但是如果您首先声明 JScript,您需要将您的 VBScript 限制为 SUB 或函数,反之亦然,否则不可预测的事情可能会发生发生。

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> <%        

%>  

回答by ALienKid

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

我有同样的问题,但很快我就想出了如何将 json 对象发布到 ASP 服务器。

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)

将 json 对象解析为字符串,然后回发到服务器。(这是典型的字符串发布)

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

在 ASP 端,使用 JSON 库将字符串解析回对象。

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

JSON 库的链接 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

'我认为尝试将 JSON 发布回 asp 的人应该具有基本的发布知识。任何进一步的帮助请让我知道

回答by Yuval

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:

通常 (new VBArray(arr).toArray()) 应该在 SafeArray 上工作,这是 Request.BinaryRead 返回的。但事实并非如此。我找到了我修改的这个解决方案:

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

感谢 Rasberry 的评论@ 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.

请注意,它可能无法正确处理编码。我没有深入研究 ADO 数据类型的 201 意味着什么,但它有效。

回答by Alex Park

You can find all posted parameters via this code.

您可以通过此代码找到所有发布的参数。

FUNCTION getQueryString()
    dim queryLink, queryItemName
    queryLink = ""
    On error Resume Next
    For each queryItemName in Request.QueryString
        Execute(queryItemName & " = stripQuery(Request.QueryString(""" & queryItemName & """))")
        queryLink = queryLink & "&" & queryItemName & "=" & Request.QueryString(queryItemName)
    Next
    For each queryItemName in Request.Form
        Execute(queryItemName & " = stripQuery(Request.Form(""" & queryItemName & """))")
        queryLink = queryLink & "&" & queryItemName & "=" & Request.Form(queryItemName)
    Next
    On Error Goto 0
    getQueryString = queryLink
END FUNCTION

response.write getQueryString()