AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 How to call webservices from remote server with java script
Posted by Ajay Bhushan on  3/28/2009 6:49:17 AM Category: ADO.NET
Add to my favorites
Email to friend
Hi,



I’m mailing script for Adserver using DotNet 2.0. Facing the problem as given below:



Created a webservice named 'WebService.cs' in my project in app_code folder, its respective .asmx file is in my main project folder(outside app_code).



Now this project is published and uploaded.



When someone logins on my website as a publisher, he gets the following script to paste



<body onLoad="return TaggedData('ws','1','PUBID01','adht','adwidth','categorycode','#f5f5dc','#ffffff','#a9a9a9','#ffffff','#006400');" >



<script src="http://www.xxx.com/script/Javascript.js" type="text/javascript"></script>

<div ID="ws" title="hello" style="BEHAVIOR:url(webservice.htc)" onresult="GetResult('Div1Resul');"></div>

<div ID="Div1Resul"></div>



Above script calls the javascript named 'Javascript.js' in my online project, this javascript in return calls my webservice named ‘WebService.cs' through the code :-



init(objwc,"http://www.xxx.com/WebService.asmx?wsdl","GetDetails")



which ultimately displays the desired records from the database. Now,



1. It does not work when the above mentioned script is called in a html/dotnet file within the same domain or any other domain. It reaches the online javascript file but then stops without displaying the record.



webservice.htc file is placed within the same folder.



It gives the error:



Object doesn’t support this property or method.

Even when webservice.htc is placed along with the script file or removed from there.



2. In the localhost path above mentioned script is placed in an aspx page. Along with this, a webservice.htc file is also placed in the same folder as the aspx page. It reaches the local javascript file, then calls the local webservice.asmx and displays the desired record.



3. It also works fine when the above mentioned script is in html format along with webservice.htc on desktop with internet connectivity.



It reaches the online javascript file, then calls the webservice.asmx file and displays the desired record.

Post Your Reply Post New Question

 Viewer's Reply
Solution: How to call webservices from remote server with java script
Answered By Moderator1 on 4/1/2009 8:01:10 PM
Check the article in the below url,
http://www.aspdotnetcodes.com/Ajax_ModalPopup_PostBack_GridView.aspx

It shows an easy way to access web service from the JavaScript. You have make use of AJAX ScriptManager as the code below,

<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="WebService.asmx" />
</Services>
</asp:ScriptManager>

Then in JavaScript, create a function to call the web method, and another javascript function to display the result from the WebService.

function fnCallMyWebMethod()
{
WebService.MyFirstWebMethod(fnDisplayResult);
}

function fnDisplayResult(result)
{
alert(result);
}
 
Add Script Services Reference in your Web Service
Answered By Moderator1 on 4/1/2009 8:23:02 PM
Hi,

In order to call the WebService, in JavaScript through ScriptManager, you have to add the below code reference in your webservice at the declaration part.

[System.Web.Script.Services.ScriptService()]

That's it hope this will help you.
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Sexy Web Design - Free 71 Page Preview!
Oracle Magazine
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!