As the title states, I’m trying to call a web service written in ASP.Net (Same solution, but different project in visual studio) from javascript. Since I added the web reference for the service prior to this for calling it in VB.Net, I tried to use this reference by directly calling it.
In the body of the Default.aspx page, I have this code:
<asp:ScriptManager id="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="~/App_WebReferences/localhost/ServiceName.discomap" InlineScript="true" /> </Services> </asp:ScriptManager>
but in javascript, I can’t call my service at all. Could anyone explain me how? I’d want to do something like this:
<script type="text/javascript"> alert(ServiceName.HelloWorld()) </script>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Finally found what I think is the right way to do it, it doesn’t need jQuery at all, nor httprequest or any weird workaround. Here’s the related code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="WebService.asmx/js" type="text/javascript"></script>
<script type="text/javascript">
function callback(msg) {
alert(msg);
};
function HelloWorld() {
WebService.HelloWorld(callback);
};
</script>
<title></title>
</head>
<body>
<div id="test" onclick="HelloWorld();">
click this
</div>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService.asmx" />
</Services>
</asp:ScriptManager>
</form>
</body>
</html>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0