Link to home
Start Free TrialLog in
Avatar of pld51
pld51

asked on

How to receive a webservice in classic ASP

We want to receive and process data sent by a third party landing page website, so we can add to a table in our database. The data, provided in both JSON and XML format, is form data sent by the landing page website every time a user fills in the landing page form.

So we need an ASP web page to receive this data. Currently, for testing purposes, the data is sent to postbin.org; here is the format of one request http://www.postbin.org/1iyz1ve.

We don't mind whether the XML or JSON is parsed, but would have slight preference for XML. Please note we are complete beginners with web services and JSON, and are only familiar with classic ASP, so please use baby talk if possible!
Avatar of Big Monty
Big Monty
Flag of United States of America image

its pretty easy to do in classic asp, here's a good tutorial explaining on how to do it:

http://successontheweb.blogspot.com/2008/04/web-services-in-classic-asp.html
Avatar of pld51
pld51

ASKER

Thanks Big Daddy! Unfortunately these tutorials don't work easily for dummies like us, I've now tried several. This one looks easy, but in our hands...

I created a send and receive ASP page, following as closely as possible the given code.

The send page is:-
<%@ Language=vbScript%>
<%
Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlDoc = Server.CreateObject("MSXML2.DOMDocument")
xmlHTTP.open "POST", "http://www.ourdomain.com/forms/test-rec3.asp", false
xmlHTTP.send "<xml_node><sent_node>node value</sent_node><xml_node>"

Set xmlhttp = nothing
Set xmlDoc = nothing
%>

Open in new window


This gives the Microsoft VBScript runtime error '800a01b6': Object doesn't support this property or method: 'xmlDoc'.

I've also set the receiving file as follows:-
<%@ Language=vbScript%>
<%
strStatus = xmlHTTP.Status
strRetval = xmlHTTP.responseText
xmlDoc.loadxml(xmlHTTP.responseText)

returnedValue = xmlDoc.getElementsByTagName("returned_node").item(0).text
If returnedValue  = "node value" Then
result = "yay!"
Else
result = "boo!"
End If

Set xmlhttp = nothing
Set xmlDoc = nothing

%>

Open in new window


Assuming this is an easy glitch to fix, what I also do not understand is, presumably on clicking the send page, what/how does the receive page work? Does it automatically open online and show the values?

thanks
You omitted a step. I've declared the variables; see if this is any better
<%@Language=VBScript%>
<%
Dim xmlHTTP
Dim xmlDoc
Dim status
Dim retval
Dim returnedValue

Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")

xmlDoc = Server.CreateObject("MSXML2.DOMDocument")

xmlHTTP.open "POST", "http://www.ourdomain.com/forms/test-rec3.asp", false '' Synchronous request
xmlHTTP.send "<xml_node><sent_node>node value</sent_node><xml_node>"

status = xmlHTTP.Status
retval = xmlHTTP.responseText

xmlDoc.loadxml(retval)

returnedValue = xmlDoc.getElementsByTagName("returned_node").item(0).text

If returnedValue  = expectedValue Then
    retval = "yay!"
Else
    retval = "boo!"
End If

Set xmlHTTP = Nothing
Set xmlDoc = Nothing
%>

Open in new window

Avatar of pld51

ASKER

Thanks Badotz, you have merged into one file both the send and receive file. What is the logic? I would have thought they should be separate. And in normal practice, not here where we are testing, on quite seperate domains.

I separated the part for send, and changed the send page to this:-

<%@Language=VBScript%>
<%
Dim xmlHTTP
Dim xmlDoc
Dim status

Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")

xmlDoc = Server.CreateObject("MSXML2.DOMDocument")

xmlHTTP.open "POST", "http://www.our domain.com/forms/test-rec3.asp", false '' Synchronous request
xmlHTTP.send "<xml_node><sent_node>node value</sent_node><xml_node>"

Set xmlhttp = nothing
Set xmlDoc = nothing
%>

Open in new window


This again threw a Microsoft VBScript runtime error in the browser: Object doesn't support this property or method in line 10.

Line 10 is:
xmlDoc = Server.CreateObject("MSXML2.DOMDocument")
Since the POST call is synchronous, you must wait for the response before continuing. That is why the "send and receive" is in the same place. It must be for the code to work.

In your code, Line 7 is not the same as your error line number - how so?
Avatar of pld51

ASKER

OK, I copied the whole code of your first reply in, and still got a  Microsoft VBScript runtime error Object doesn't support this property or method, for line 11.

This is xmlDoc = Server.CreateObject("MSXML2.DOMDocument"), as before. (You ask re line numbers, difference is due to some spaces in the test doc which I ignored when copying into this).
Verify that you have MSXML 3.0 (or higher) installed on your web server. You can get the latest version of the MSXML Parser at the Microsoft website.
Avatar of pld51

ASKER

we use a hosting company, so can't really mess around with what they have installed
Don't you issue xmlDoc = Server.CreateObject("MSXML2.DOMDocument") on your server?
Avatar of pld51

ASKER

Just checking that with the host company. Does it make any difference to this question if what we are trying to do is handle a webhook? Perhaps this is different from a web service. Each time a user fills in a form, the landing page company uses the webHook as an HTTP callback: an HTTP POST is then sent to the URL we give. So what we need is a page that handles this.

To quote  http://wiki.webhooks.org "handling the POST data becomes fairly trivial". Ha ha!!
>>Just checking that with the host company.

What on earth for? Don't you run this code locally
<%@Language=VBScript%>
<%
Dim xmlHTTP
Dim xmlDoc
Dim status
Dim retval
Dim returnedValue

Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")

'' @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'' @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

'' Verify that you have MSXML 3.0 (or higher) installed

xmlDoc = Server.CreateObject("MSXML2.DOMDocument")

'' @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'' @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

xmlHTTP.open "POST", "http://www.ourdomain.com/forms/test-rec3.asp", false '' Synchronous request
xmlHTTP.send "<xml_node><sent_node>node value</sent_node><xml_node>"

status = xmlHTTP.Status
retval = xmlHTTP.responseText

xmlDoc.loadxml(retval)

returnedValue = xmlDoc.getElementsByTagName("returned_node").item(0).text

If returnedValue  = expectedValue Then
    retval = "yay!"
Else
    retval = "boo!"
End If

Set xmlHTTP = Nothing
Set xmlDoc = Nothing
%>

Open in new window

Avatar of pld51

ASKER

I am not sure if this is the answer to your point: we have never managed to do local testing because configuring IIS was beyond us the few times we tried it. So we upload all test pages, including this, to our hosting realtime website and test it there with non-public URLs.

Is this your point, or are you saying that, despite this, I still need to have MSXML 3.0 on my own laptop??
>>...we have never managed to do local testing...

Then you are at your host's mercy. They might not even be a Microsoft shop.

Nothing more I can do for you, sorry.
Avatar of pld51

ASKER

http://www.alentus.com/ is who we use
Avatar of pld51

ASKER

but thanks for your help anyway, appreciated
They should have the tools you need.

No worries - glad to help - or not :-(
Avatar of pld51

ASKER

Well they finally answered. The code was wrong, "Set" is missing.

It should be:-
Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument")

So with this corrected, now there is an error on line 29 above:-
Object required: 'getElementsByTagName(...).item(...)'

So I come to the possible conclusion that this is not the fault of the host, nor my web server, but of sloppy coding in tutorials. Hard to believe. Or am I wrong?
SOLUTION
Avatar of Badotz
Badotz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pld51

ASKER

Thanks for sticking with this!

I made the change as indicated, both as two lines as you show above, and as one line. Both gave error message. Here is the message for the one line version:-
returnedValue = xmlDoc.getElementsByTagName("returned_node")[0].childNodes[0].nodeValue;


Microsoft VBScript compilation error '800a0401'

Expected end of statement

/forms/test-send3b.asp, line 23

returnedValue = xmlDoc.getElementsByTagName("returned_node")[0].childNodes[0].nodeValue;
------------------------------------------------------------^
It isn't a "two-line methos", it is supposed to be on a single line.

Remove the semi-colon at the end of the line (carryover from JavaScript code).
Avatar of pld51

ASKER

I'm still getting the "Expected end of statement" error where the dotted line underneath points to the first bracket before 0:-

returnedValue = xmlDoc.getElementsByTagName("sent_node")[0].childNodes[0].nodeValue
---------------------------------------------------------------------------------------------^

Note I didn't know how/if to change to the XML format you sent. So the line above sending the message remains that of the tutorial:-
xmlHTTP.send "<xml_node><sent_node>node value</sent_node><xml_node>"

In the returnedValue statement above I tried both "sent_node" and "returned_node". Same error.
The format of the XML was for reference only, and to show the structure to which the code refers.

This is the code I used:
to = xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;

Open in new window

If the structure of the XML you receive differs from that in my example, kindly show it to me.
Avatar of pld51

ASKER

Perhaps best to show the testcode again, as the problem seems not to be XML, but line 21. This code is the version you provided, adjusted for errors.

<%@Language=VBScript%>
<%
Dim xmlHTTP
Dim xmlDoc
Dim status
Dim retval
Dim returnedValue

Set xmlHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP")

Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument")

xmlHTTP.open "POST", "http://www.latindiscover.com/forms/test-rec3.asp", false '' Synchronous request
xmlHTTP.send "<xml_node><sent_node>node value</sent_node><xml_node>"

status = xmlHTTP.Status
retval = xmlHTTP.responseText

xmlDoc.loadxml(retval)

returnedValue = xmlDoc.getElementsByTagName("sent_node")[0].childNodes[0].nodeValue

If returnedValue  = "node value" Then
    retval = "yay!"
Else
    retval = "boo!"
End If

Set xmlHTTP = Nothing
Set xmlDoc = Nothing
%>

Open in new window

It would help tremendously if you would supply a sample of the XML you expect to receive from the web service. Without that, I'm afraid I won't be of much help.
Avatar of pld51

ASKER

I think we have problems here, as I was following your suggestion, and assumed you had a clear goal in mind.

I started by taking the suggested tutorial at the very top, http://successontheweb.blogspot.com/2008/04/web-services-in-classic-asp.htm, and trying to make that work. It seemed to have a send page, and a receive page. You then said this should be merged into one page, which I still don't understand.

Throughout all of this it looks as if the data being sent is in this line:-
xmlHTTP.send "<xml_node><sent_node>node value</sent_node><xml_node>"

So I was expecting the solution to be able to obtain node value as the value.
>>You then said this should be merged into one page, which I still don't understand.

You are making a synchronous POST (the 3rd var in the "xmlHTTP.open" statement is false), which means you must WAIT for the results before continuing, otherwise they will be LOST.

If you wish to continue and be notified when the data returns, then you can make an asynchronous POST (change the 3rd var in the "xmlHTTP.open" statement to true) and the results can be returned through a callback method (that you must specify).

>>Throughout all of this it looks as if the data being sent is in this line

What you SEND may - or may not - have any bearing on what you RECEIVE. Hence my asking for a sample of the received XML - specifically the "responseText" - that gets stuffed into "retval" in this command
retval = xmlHTTP.responseText

Open in new window

.

>>So I was expecting the solution to be able to obtain node value as the value.

Again, what you submit to the web service is irrelevant. What I am asking for is a sample of the XML returned to you by the web service.
Avatar of pld51

ASKER

That's where there seems a problem in communication. I mentioned above that the testcode has a problem: it keeps throwing out an error.

This error is the "Expected end of statement" error where the dotted line underneath points to the first bracket before 0:-

returnedValue = xmlDoc.getElementsByTagName("sent_node")[0].childNodes[0].nodeValue
---------------------------------------------------------------------------------------------^

A couple of lines before this is the line
retval = xmlHTTP.responseText

As it is now, nothing is shown for this before the error message. How to see? How to solve the error message?
Have you no idea what the returned XML (from the web service) looks like?

How can you know what to look for IF YOU DON'T KNOW THE FORMAT OF IT?

Did they not supply you with a sample so you would know where the data you want resides?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
padas: Would missing data cause a syntax error (Expected end of statement)?

I think not...
As a matter of fact it could very well be the problem.   Or there is no such thing as "sent_node" in the xml file.  

I see name, ip_address, destination, campaign, phone, source, email.  

Whenever I do this, I ran into something similar and now I always test if the node exists and the problems go away.  I would give it a try.
Well, I'll be dipped! Are you telling us that a lack of data can cause a syntax error?

During my career, I have not found that to be true. And yet...

I'd like to see another example of this, not from code generated by data, but where data - or the lack of data - causes a syntax error in the code.
You are correct sir.  I just got ahead of myself.

The problem are those brackets need to be parentheses.  

xmlDoc.getElementsByTagName("sent_node")[0].childNodes[0].nodeValue
xmlDoc.getElementsByTagName("sent_node")(0).childNodes(0).nodeValue

You still should test for no data though.
padas: Good catch on the parens. All points to you ;-)

re: Test for data - What is returned if a node doesn't exist? NULL or Nothing or...
Exactly.  Because the next error he may get will have to do with no data.  

For each known node something like below or whatever is needed to populate each field.

name, ip_address, destination, campaign, phone, source, email.  

if objReturn.getElementsByTagName("destination").length>0 then
        destination =objReturn.getElementsByTagName("destination").Item(0).Text
        else
         destination=""
end if

Open in new window

My question remains: What is returned if a node doesn't exist?
If the node does not exist the else statement will be returned. I am using this now on a weather feed where nodes will not exist when there is no data.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pld51

ASKER

Unfortunately I never got this to work, reasons unknown. Our programming level may be part of the problem. Thanks for the help, I will try again sometime in future.