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

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


'Create a functionFunction ASPPostJSON(url)'declare a variableDim objXmlHttpSet objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")'If the API needs userName and Password authentication then pass the values hereobjXmlHttp.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 serverobjXmlHttp.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 serverResponse.write(ASPPostJSON)Set objXmlHttp = NothingEnd Function'call the function and pass the API URLcall ASPPostJSON("https://TheAPIUrl.com/")


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:

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

Hope that helps someone else along the way.


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

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

Good luck!