nkman
nkman

Reputation: 545

How to send Json object (or string data) from xmlhttprequest using Classic ASP?

I wrote an ASP Script to generate JSON data/string. How do i send that data to a web service? I was given the info below, and i do not have access to the server to register any dll files. I did some searching and saw that i should be using XMLHttpRequest but not sure how to do that.

Please help. Thank you.

Connecting to the web service The web service works over the HTTP protocol. It is recommended that this web service, once in production, use a Secure Socket Layer (HTTPS). The web service is designed to work with the URL: http://thedomain.com/api/push

The site uses an authentication token in the header to prevent crawlers from interfering with the web service. When sending data add a header called HTTP_TOKENKEY with the value of ABCDEFGHIJKL. This is one reason why HTTPS is recommended. The web service will look for JSON data in the request’s POST and sends JSON responses back to the client.

This is what i have so far


strJSONToSend = theevent 

webserviceurl = "http://thedomain.com/api/push"

Set objRequest = Server.createobject("MSXML2.XMLHTTP.3.0") 
objRequest.open "POST", webserviceurl, False 

objRequest.setRequestHeader "Content-Type", "application/json; charset=UTF-8" 
objRequest.setRequestHeader "HTTP_TOKENKEY","ABCDEFGHIJKLMNOPQ" 
objRequest.setRequestHeader "SOAPAction", webserviceurl

results = objRequest.send (strJSONToSend)
write (results)

set objJSONDoc = nothing 
set objResult = nothing

this freezes up and does nothing

Upvotes: 4

Views: 4771

Answers (1)

Nathan Rice
Nathan Rice

Reputation: 3111

Here is some code I published before at this URL adapted for your situation:

http://naterice.com/articles/69

strJSONToSend = theevent 
webserviceurl = "http://thedomain.com/api/push"

sResponseHTML = GetHTTP(strJSONToSend, webserviceurl)
If len(HTTPErrorHandeler) > 0 Then
  strResponse = HTTPErrorHandeler
Else
  strResponse = sResponseHTML
End If

Response.Write strResponse

Function GetHTTP(sSendHTML, sURL)
  'This script is provided under the Creative Commons license located'
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not'
  'be used for commercial purposes with out the expressed written consent'
  'of NateRice.com'
  Set oHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
  oHTTP.Open "POST", sURL, false
  oHTTP.setRequestHeader "Content-Type", "application/json; charset=UTF-8" 
  oHTTP.setRequestHeader "HTTP_TOKENKEY","ABCDEFGHIJKLMNOPQ" 
  oHTTP.setRequestHeader "SOAPAction", webserviceurl

  On Error Resume Next
  oHTTP.send sSendHTML
  sHTTPResponse = oHTTP.responseText

  If Err.Number = 0 Then
    GetHTTP = sHTTPResponse
  Else
    GetHTTP = HTTPErrorHandeler
  End If
  On Error Goto 0

  Set oHTTP = Nothing
End Function


Function HTTPErrorHandeler
  'This script is provided under the Creative Commons license located'
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not'
  'be used for commercial purposes with out the expressed written consent'
  'of NateRice.com'
  If Err.Number <> 0 Then
  HTTPErrorHandeler = "ERROR <br />" & _
             "  ERR Number: " & Err.Number & " <br />" & _
             "  ERR Description: " & Err.Description
  Else
  HTTPErrorHandeler = ""
  End If
End Function

Upvotes: 2

Related Questions