Seeking a solution to call an ASP.NET .asmx webservice from JavaScript using a namespace different from the default one set by Visual Studio during creation.
Upon using the Visual Studio wizard to generate a webservice named Hello in the WebServices folder, this code snippet is produced:
namespace MyWebSite.WebServices
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class Hello : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
The JavaScript on the browser interacts with it like so:
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
ServiceReference serviceReference = new ServiceReference();
serviceReference.Path = "~/WebServices/Hello.asmx";
serviceReference.InlineScript = false;
scriptManager.Services.Add(serviceReference);
To make the call from JavaScript:
MyWebSite.WebServices.Hello.HelloWorld(function(rval){{alert(rval);}});
However, the issue arises when my calling JavaScript exists in a separate assembly within a server control and expects a different namespace. The dilemma pushes me towards exploring a change in the webservice's namespace to echo that of the call.
I thought simply altering the namespace in the .asmx file would suffice:
namespace OtherNamespace.WebServices
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
...
Unfortunately, this adjustment doesn't resolve the issue, as I encounter an "OtherNamespace is not defined" error. This discrepancy highlights that while the namespace in the .asmx file aligns with the prefix in the JS call, they don't reference the same entity.
Where does the prefix MyWebSite.WebServices. in the JavaScript call originate? How can its definition be altered to reflect something different?