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

时间:2011-11-15 23:01:16

标签: json asp-classic xmlhttprequest

我完全迷失了。我试图从使用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=asdf@hotmail.com&firstname=joe&lastname=smith"
vAnswer = xmlhttp.responseText  

我收到的回复是请求不是预期的格式。技术支持告诉我,API期望在帖子正文中使用JSON。我可以从服务器端asp执行此操作吗?

4 个答案:

答案 0 :(得分:3)

因为我在有效地尝试解决同样的问题时遇到了这个问题 - 在我的案例中,从ASP Classic发布到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

希望在此过程中帮助其他人。

答案 1 :(得分:2)

'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"": ""test@mail.com""}}"

'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/")

答案 2 :(得分:1)

检查一下,它应该可以帮助你做你想做的事情:

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

祝你好运!

答案 3 :(得分:1)

您发送的请求是.....而不是JSON。尝试将其用作验证器:JSONLint。将你的JSON字符串放入那里,它会告诉你它是否有效。在上面的例子中:email=asdf@hotmail.com&firstname=joe&lastname=smith。绝对不是。

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

{"Email":"asdf@hotmail.com", "firstname":"joe", "lastname":"smith"}

我希望有所帮助。是的,有些库可以帮助你做到这一点(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