• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1228
  • Last Modified:

Javascript accessing a Webservice

I am trying to access a webservice using Javascript, but keep getting a result of undefined.  Can you tell me what is wrong with the following?
-------------
<html>

<head>
<title>JavaScript</title>
<script language="javascript" type="text/javascript">
function callWebService(f) {
WebService.useService("http://webservices.imacination.com/distance/Distance.jws?wsdl","DistanceService");
WebService.DistanceService.callService(callbackFunction, "getDistance", parseInt(f.elements["lower"].value), parseInt(f.elements["upper"].value));
}
function callbackFunction(result){
document.getElementById("random").innerHTML=result.value
}
</script>
</head>

<body>
<div id="WebService" style="behavior:url(webservice.htc);"></div>
<form>
      The distance between
      <input type="text" name="lower" size="3"
       value="65775" /> and
      <input type="text" name="upper" size="3"
      value="65606" /> is
      <span id="random" /><br />
      <input type="button" value="Retrieve"
      onclick="callWebService(this.form);" />
</form>
</body>

</html>
-------------------
Here is the link to the service information: http://webservices.imacination.com/distance/
Thank you
0
dale4brumbaugh
Asked:
dale4brumbaugh
  • 6
  • 2
1 Solution
 
SirCroftyCommented:
Where is the WebService object and the variable callbackFunction defined? It seems like you might need a link to a js file that contains additional code.
0
 
SirCroftyCommented:
Terribly sorry, I didn't read this closely - you've defined both. Are you trying to call a web service from outside your domain? This may lead to problems using the WebService object. Also, you might want to check onload to make sure the service you are calling is available.

Also, I believe you can change this line:
WebService.DistanceService.callService(callbackFunction, "getDistance", parseInt(f.elements["lower"].value), parseInt(f.elements["upper"].value));

to this:
DistanceService.callService(callbackFunction, "getDistance", parseInt(f.elements["lower"].value), parseInt(f.elements["upper"].value));
0
 
dale4brumbaughAuthor Commented:
If I use your line I get a message that the object does not support this method.  Using my line I just get an undefined result.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
SirCroftyCommented:
Did you download the htc file for the WebService behavior from msdn and copy it into your web project folder?

http://msdn.microsoft.com/archive/en-us/samples/internet/behaviors/library/webservice/webservice.htc
0
 
SirCroftyCommented:
If you do have the htc file, then what happens when you change the callback method to the following?

function callbackFunction(result)
{
if(result.error)
{
      alert("Error");
      alert(result.errorDetail.string);
      alert(result.errorDetail.code);
}
else
{
document.getElementById("random").innerHTML=result.value
}
}
0
 
SirCroftyCommented:
When I run this I get an Invalid active port specified. Looking through the wsdl and the htc file, I see there is a difference between the namespace definitions - the service has used wsdlsoap, while the htc file just uses soap. So the htc file tries to process

soap:body

but the service has it defined as wsdlsoap:body.

This could be causing your problem. I changed all the instances of soap: to wsdlsoap: in the htc file and the service returned an error of no SOAPAction header. I think you need to use the optional call elements to set a valid SOAPAction in the SOAPHeader.

Hope that helps.
0
 
dale4brumbaughAuthor Commented:
Thank you SirCrofty.  I get a "service unavailalbe" and "client" message using the error checking you gave me.  Do you get the same thing?
0
 
SirCroftyCommented:
I get "Client" and "Invalid Active Port Specified" - this is due (I believe) to the discrepancies I described in the htc file and the wsdl from the third party source. The error you are experiencing is most likely because:

- The service itself was not available, and therefore you cannot connect
- Your not utilizing a proxy to make your request

If you try again today and the service is available, then you know that the service was down. However, if you continue to get that error, it may be that you are not using a proxy even though you are making a cross domain request. In that case I would recommend running the code from your web server to test.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now