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

标签 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 执行此操作吗?

最佳答案

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

关于json - 使用 xmlhttp 的经典 ASP 页面可以发出 JSON 请求吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8144368/

相关文章:

javascript - python3.5 aiohttp,请求post,json格式和405错误

dictionary - 如何通过给定的项目值显示 Motobit Multi.Dictionary 中的键?

javascript - 状态为 0 的 XMLHttpRequest

authentication - 使用 XMLHttpRequest 的用户身份验证适用于 IE,而不适用于 Chrome?

javascript - 解析 JSON 子元素时出现问题

ios - 我如何根据用户的回答为常见问题创建模型?

JSON 模式,oneOf 验证

javascript - JSON 解析问题经典 ASP

mysql - 服务器对象错误 'ASP 0177 : c0000005'

jquery - 缺少“访问控制允许来源”。我的下一步应该是什么?