leskelly
asked on
Error when calling Web Service from JavaScript in .NET application
I have a basic ASP.NET web project that contains nothing but a web form with a HTML button that when clicked calls a JavaScript function that calls a web service. My code is below.
HTML:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx. vb" Inherits="WebServiceTest.W ebForm1" %>
<!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">
<title></title>
</head>
<body>
<script src="Script/TestJavaScript .js" type="text/javascript"></s cript>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebServices/TestWe bService.a smx" />
</Services>
</asp:ScriptManager>
<div>
<input id="Button1" type="button" value="button" onclick="TestFunction()"/>
</div>
</form>
</body>
</html>
JavaScript:
function TestFunction()
{
alert(HelloWorld());
}
Visual Basic:
Imports System.Web.Services
Imports System.Web.Services.Protoc ols
Imports System.ComponentModel
<System.Web.Script.Service s.ScriptSe rvice()> _
<System.Web.Services.WebSe rvice(Name space:="Te stNameSpac e")> _
<System.Web.Services.WebSe rviceBindi ng(Conform sTo:=WsiPr ofiles.Bas icProfile1 _1)> _
<ToolboxItem(False)> _
Public Class WebService1
Inherits System.Web.Services.WebSer vice
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
End Class
When I click the button I get the message:
"Line: 3
Error: Object expected"
This is in reference to line three of the JavaScript function "alert(HelloWorld());" so it is not finding the Web Service function.
Adding the class to the call "alert(WebService1.HelloWo rld());" results in the message:
"Line: 3
Error: 'WebService1' is undefined"
Adding the name space to the call "alert(TestNameSpace.WebSe rvice1.Hel loWorld()) ;" results in the message:
"Line: 3
Error: 'TestNameSpace' is undefined"
Any suggestions would be greatly appreciated.
HTML:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.
<!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">
<title></title>
</head>
<body>
<script src="Script/TestJavaScript
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebServices/TestWe
</Services>
</asp:ScriptManager>
<div>
<input id="Button1" type="button" value="button" onclick="TestFunction()"/>
</div>
</form>
</body>
</html>
JavaScript:
function TestFunction()
{
alert(HelloWorld());
}
Visual Basic:
Imports System.Web.Services
Imports System.Web.Services.Protoc
Imports System.ComponentModel
<System.Web.Script.Service
<System.Web.Services.WebSe
<System.Web.Services.WebSe
<ToolboxItem(False)> _
Public Class WebService1
Inherits System.Web.Services.WebSer
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
End Class
When I click the button I get the message:
"Line: 3
Error: Object expected"
This is in reference to line three of the JavaScript function "alert(HelloWorld());" so it is not finding the Web Service function.
Adding the class to the call "alert(WebService1.HelloWo
"Line: 3
Error: 'WebService1' is undefined"
Adding the name space to the call "alert(TestNameSpace.WebSe
"Line: 3
Error: 'TestNameSpace' is undefined"
Any suggestions would be greatly appreciated.
ASKER
Hello and thanks for the response. I changed the class name to TestWebService but got the same results.
When you say to, "make sure the URL works" are you referring to the path , "~/WebServices/TestWebServ ice.asmx"? If so I believe it does as intellisense recognizes it.
When you say to, "make sure the URL works" are you referring to the path , "~/WebServices/TestWebServ
Yeah I meant that. You may want to experiment with the path for example
"/WebServices/TestWebServi ce.asmx"
"/WebServices/TestWebServi
ASKER
I tried removing the leading ~ and then the / but got the same results. Any other suggestions?
ASKER
I haven't heard back, do you have anything else for me?
Hi,
You can not directly call the webservice like a function in Javascript.
You have to call it using Ajax or Services method
Simple Method
http://www.codeproject.com/Articles/14610/Calling-Web-Services-from-HTML-Pages-using-JavaScr
and using ajax
http://www.codeproject.com/Questions/334158/Call-Webservice-using-javascript
You can not directly call the webservice like a function in Javascript.
You have to call it using Ajax or Services method
Simple Method
http://www.codeproject.com/Articles/14610/Calling-Web-Services-from-HTML-Pages-using-JavaScr
and using ajax
http://www.codeproject.com/Questions/334158/Call-Webservice-using-javascript
ASKER
Hello darjimaulik,
Thanks for your response. My code is using AJAX hence the ScriptManager in the aspx file. I tried creating new projects that are as identical as possible in three different formats
a .NET 3.5 Web Site
a .NET 4.0 Web Site
and a .NET 4.0 Web Application.
The first two worked and the third did not. My original project was a .NET 4.0 Web Application and I've successfully used web services in other Web Site projects. This lead me to believe that the problem was in the different ways that Web Sites and Web Projects handle the code behind page of the asmx file.
A created a App_Code folder in my .NET 4.0 Web Application and placed a .vb file in it that contained the same code as the asmx code behind file. I then changed the mark up of the asmx file so that the vb file was given as the code behind file. It then worked. It was just a simple "Hello World" app so I will need to look at it some more to see if this solution works with more complex situation. I won't be able to get back to this until next week but when I do I will update this post in case anyone is interested.
Thanks for your response. My code is using AJAX hence the ScriptManager in the aspx file. I tried creating new projects that are as identical as possible in three different formats
a .NET 3.5 Web Site
a .NET 4.0 Web Site
and a .NET 4.0 Web Application.
The first two worked and the third did not. My original project was a .NET 4.0 Web Application and I've successfully used web services in other Web Site projects. This lead me to believe that the problem was in the different ways that Web Sites and Web Projects handle the code behind page of the asmx file.
A created a App_Code folder in my .NET 4.0 Web Application and placed a .vb file in it that contained the same code as the asmx code behind file. I then changed the mark up of the asmx file so that the vb file was given as the code behind file. It then worked. It was just a simple "Hello World" app so I will need to look at it some more to see if this solution works with more complex situation. I won't be able to get back to this until next week but when I do I will update this post in case anyone is interested.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
One additional point the files placed in in App_Code will a automatically have a build action of Content so intellisense won't work. To correct this right click on each file, got to properties and change the build action to Compile.
ASKER
I found my own solution.
>Public Class WebService1
Make both the filename and classname same. Also make sure the URL works.