使用xmlhttp的传统ASP页面可以发出JSON请求吗? [英] Can a classic ASP page using xmlhttp make a JSON request?
本文介绍了使用xmlhttp的传统ASP页面可以发出JSON请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我完全迷路了。我正在尝试从使用VBScrip的经典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/")
这篇关于使用xmlhttp的传统ASP页面可以发出JSON请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文