Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript & Webservice - problem with webservice.htc ?

Status
Not open for further replies.

mmy1981

Programmer
Apr 2, 2007
164
BE
Hi,

I'm trying to consume a webservice (default HelloWorld created in Visual Studio 2010 / ASP.NET web application)

The .asmx file is located on my local network. When I enter the URL in my browser ( I see the webservices, ... So I'm sure this page is ok.

Now I'm trying to consume this webservice using an HTML doc with JavaScript.

I looked for some tutorials on the net, all the examples say I should place "webservice.htc" in the same folder as my HTML doc (and refer to it using the 'id' and 'style' attribute in an element - I used it in the body tag)

However, when calling ".useService" I receive an error "TypeError: Object doesn't support property or method 'useService'" (tested with IE9, Chrome16, FF5.0)

I looked into the webservice.htc file (version 1.0.1.810) and there is a public method called useService.

This is my testing code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test service</title>
<script type="text/javascript" language="javascript">

function init()
{
	try
	{
		websrvCaller.useService("[URL unfurl="true"]http://intranet/testservices/service1.asmx?wsdl","myService");[/URL]
	}
	catch (e)
	{
		alert(e);	
	}
}

function test()
{
	websrvCaller.myService.callService(myCallback,"HelloWorld", "Asynchronous Call");	
}

function myCallback(res)
{
	if (!res.error) 
	{
    	alert("Successful call. Result is " + res.value);
  	}
  	else 
	{
    	alert("Unsuccessful call. Error is " + res.errorDetail.string);
  	}	
}
</script>

</head>

<body ID="websrvCaller" onload="init()" STYLE="behavior:url(webservice.htc);" >
	<input type="button" id="btnTest" value="test" onclick="test();" />
</body>
</html>

The first init() service triggers the error... Anybody knows what I'm missing here?
 
EDIT: it seems the problem is caused that the webservice is located on another server. When I use the same webserver (Win server with IIS) for the HTML files and webservice they seem to work ok.

test fails when:
- html = located on my laptop, running a wamp webserver
- webservice = located on a windows server running iis (the server and my laptop are in the same local network)

Maybe these are security settings?
 
EDIT: sorry, but seems I was wrong in my last reply.
I can work on different servers (my webpage on my laptop with wamp or IIS and the webservice on a IIS server).

But when I launch my webpage from dreamweaver (CS3) - preview in browser, I receive the error.

If I enter the URL of the webpage, everything works fine (although I have to allow running ActiveX or scripts on the page, but this must be a security setting in the IE options). If I don't allow this, the page won't run the script (no errors neither caus the script isn't executed)

When I run the page from dreamweaver (the preview), the warning (ActiveX or scripts) isn't displayed, but I receive the error (so, the script is executed)

Still don't know why this preview won't work...
 
EDIT: sorry, but seems I was wrong again in my last reply.
it seems the script only runs when I double click the html file (on my laptop or webserver, depending where it's located). When I open the page typing the URL ( I receive an error (all the HTML objects are ok, so I can load the page)
 
EDIT2: ok...one step further -> seems the first rule
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
is causing the error. When I remove this, everything works fine....For IE only! Seems FF, Chrome, ... can't just use JavaScript to interact with webservices. Search on google says you should use AJAX in ASP.NET or jquery. So, I'm going to study jquery for this one...
 
Hi

mmy1981 said:
Seems FF, Chrome, ... can't just use JavaScript to interact with webservices.
The [tt]behavior[/tt] property and the .htc file format are Explorer only. They will never work in other browsers.


Feherke.
 
Indeed, it took me 3,5 days to figure that out. I found some reference to moz-behaviors.xml, but this is more like a workaround to get it working with FF.

I guess a better option is
1) to use ASP.NET and AJAX
2) to use JQuery or JSON (don't know if these 2 are the same. I have to get to know them first (studying the basics before I can get into the calling webservices part)

Since I'm trying to use just HTML and JavaScript (not ASP.NET), option 2 will be the best way
 
Hi

mmy1981 said:
2) to use JQuery or JSON (don't know if these 2 are the same.
[ul]
[li]jQuery is a JavaScript framework, its main benefit are simplifying frequent tasks and hiding browser specific differences[/li]
[li]JSON is just the syntax of JavaScript object literals[/li]
[/ul]
So definitely not the same, jQuery is essentially code and JSON is data. But being both related to JavaScript, evidently they are complementary in some aspect : jQuery has an AJAX module and AJAX frequently transfers data in JSON format.

Note that for AJAX to work the document in which the JavaScript code was embedded and the remote host must be in the same domain, or CORS has to be enabled.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top