gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:08:26 27 4
gpt4 key购买 nike

我完全迷失了。我正在尝试从使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com