使用 xmlhttp 的经典 ASP 页面可以发出 JSON 请求吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8144368/
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-09-03 18:03:39  来源:igfitidea点击:

Can a classic ASP page using xmlhttp make a JSON request?

jsonasp-classicxmlhttprequest

提问by user1048348

I am completely lost. I am trying to post to an API on a remote server from a classic asp page that uses vbscript. My code:

我完全迷失了。我正在尝试从使用 vbscript 的经典 asp 页面发布到远程服务器上的 API。我的代码:

set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") 
xmlhttp.open "POST", vURL, false 
xmlhttp.setRequestHeader "Content-type","application/json"
xmlhttp.setRequestHeader "Accept","application/json"
xmlhttp.send "[email protected]&firstname=joe&lastname=smith"
vAnswer = xmlhttp.responseText  

I receive a response that the request is not in the expected format. Tech support informs me that the API expects JSON in the post body. Can I do this from server-side asp?

我收到一个响应,说请求不是预期的格式。技术支持通知我 API 需要在帖子正文中使用 JSON。我可以从服务器端 asp 执行此操作吗?

回答by Anonymous

'Create a function
Function ASPPostJSON(url)

'declare a variable
Dim objXmlHttp

Set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")

'If the API needs userName and Password authentication then pass the values here
objXmlHttp.Open "POST", url, False, "User123", "pass123"
objXmlHttp.SetRequestHeader "Content-Type", "application/json"
objXmlHttp.SetRequestHeader "User-Agent", "ASP/3.0"

'send the json string to the API server
objXmlHttp.Send "{""TestId"": 012345,""Test1Id"": 123456,""Test123"": 37,""Type123"": ""Test_String"",""contact"": {""name"": ""FirstName LastName"",""Organization"": ""XYZ"",""phone"":""123456"",""emailAddress"": ""[email protected]""}}"

'If objXmlHttp.Status = 200 Then
    ASPPostJSON = CStr(objXmlHttp.ResponseText)
'end if

'return the response from the API server
Response.write(ASPPostJSON)
Set objXmlHttp = Nothing

End Function

'call the function and pass the API URL
call ASPPostJSON("https://TheAPIUrl.com/")

回答by Bret

Because I ran across this when effectively trying to solve the same issue -- in my case, POSTing from ASP Classic to the MailChimp 2.0 API -- I wanted to echo the helpfulness of the http://code.google.com/p/aspjson/link, but also note something that was at least relevant in the case of MailChimp. I thought I could simply format a JSON-looking string and send that, but it didn't work. I had to create a JSON object using the methods in the aspjson library, and then use the jsString method in the send statement. So the code snippet (after appropriate declarations, includes, etc.) would look something like this:

因为我在有效地尝试解决同一问题时遇到了这个问题 - 在我的情况下,从 ASP Classic POST 到 MailChimp 2.0 API - 我想回应http://code.google.com/p/ aspjson/链接,但也要注意一些至少与 MailChimp 相关的内容。我以为我可以简单地格式化一个看起来像 JSON 的字符串并发送它,但它没有用。我必须使用 aspjson 库中的方法创建一个 JSON 对象,然后在 send 语句中使用 jsString 方法。所以代码片段(在适当的声明、包含等之后)看起来像这样:

Set objJSON = jsObject()
objJSON("apikey") = "MY API KEY"
Set objJSON("email") = jsObject()
objJSON("email")("email") = strEmail
Set objXMLhttp = Server.Createobject("MSXML2.ServerXMLHTTP")
objXMLhttp.open "POST","https://mailchimpurl/2.0/helper/lists-for-email", false
objXMLhttp.setRequestHeader "Content-type","application/json"
objXMLhttp.setRequestHeader "Accept","application/json"
objXMLhttp.send objJSON.jsString
strResponse = objXMLhttp.responseText
Set objXMLhttp = Nothing

Hope that helps someone else along the way.

希望能帮助其他人。

回答by Bugget

Check this out, it should help you do what you want:

看看这个,它应该可以帮助你做你想做的事:

http://code.google.com/p/aspjson/

http://code.google.com/p/aspjson/

Good luck!

祝你好运!

回答by MikeMurko

The request you are sending is ..... not JSON. Try use this as a validator: JSONLint. Chuck your JSON string into there and it will tell you if it's valid or not. In the case above: [email protected]&firstname=joe&lastname=smith. It is most definitely not.

您发送的请求是 ..... 不是 JSON。尝试将其用作验证器:JSONLint。将您的 JSON 字符串放入那里,它会告诉您它是否有效。在上述情况下:[email protected]&firstname=joe&lastname=smith. 绝对不是。

You can write JSON by hand, for example I would rewrite your query as follows:

您可以手动编写 JSON,例如我会按如下方式重写您的查询:

{"Email":"[email protected]", "firstname":"joe", "lastname":"smith"}

{"Email":"[email protected]", "firstname":"joe", "lastname":"smith"}

I hope that helps. Yes, there are libraries that can help you do this (ASPJSON is one of them) but to be honest I prefer writing them out myself (ASP is so unwieldy) or writing my own functions because I know that I can trust them. Here is an example piece of code I wrote in ASP that can make a JSON string from a Dictionary object. It can also have arrays inside the dictionary elements. Unfortunately it's not recursive so it can't do arrays of arrays or dictionaries of dictionaries ... but it works quiet well for simple inputs. Named json_encode after the PHP function.

我希望这有帮助。是的,有一些库可以帮助您做到这一点(ASPJSON 就是其中之一),但老实说,我更喜欢自己编写它们(ASP 非常笨拙)或编写自己的函数,因为我知道我可以信任它们。这是我用 ASP 编写的一段示例代码,它可以从 Dictionary 对象生成 JSON 字符串。它还可以在字典元素中包含数组。不幸的是,它不是递归的,所以它不能做数组数组或字典字典......但它对于简单的输入工作得很好。在 PHP 函数后命名为 json_encode。

Function json_encode(ByVal dic)
    ret = "{"
    If TypeName(dic) = "Dictionary" Then
        For each k in dic
            Select Case VarType(dic.Item(k))
                Case vbString
                    ret = ret & """" & k & """:""" & dic.Item(k) & ""","
                Case Else
                    If VarType(dic.Item(k)) > vbArray Then
                        ret = ret & """" & k & """:["
                        For x = 0 to Ubound(dic.Item(k), 1)
                            ret = ret & """" & dic.Item(k)(x) & ""","
                        Next
                        ret = Left(ret, Len(ret) - 1)   'Trim trailing comma
                        ret = ret & "],"
                    Else
                        ret = ret & """" & k & """:""" & dic.Item(k) & ""","
                    End If
            End Select
        Next
        ret = Left(ret, Len(ret) - 1)   'Trim trailing comma
    End If
    ret = ret & "}"
    json_encode = ret
End Function