Hi. I tried to create this by following the video at http://www.asp.net/learn/videos/video-7026.aspx where Joe Stagner created a simple web service that is called by Ajax. In the Button1_onclick() handler, javascript can't resolve the object "WebService1". Please have a look and see if there is anything wrong.
The exact error is "'WebService1' is undefined" in Button1_onclick().
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxTest._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">
<title></title>
<script language="javascript" type="text/javascript">
// <!CDATA[
function Button1_onclick() {
ret = WebService1.HelloWorld(document.getElementById("Text1").value, OnComplete, OnError);
}
function OnError() {
alert("An error occurred");
}
function OnComplete(arg) {
document.getElementById("CallResponse").innerHTML = arg;
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<div>
<input id="Text1" type="text" /><br /><br />
<input id="Button1" type="button" value="Click to test Ajax" onclick="return Button1_onclick()" /><br />
<div id="CallResponse">
</div>
</div>
</form>
</body>
</html>
Here's the web service. Yes, I un-commented the line I was supposed to.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace AjaxTest
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld(string s)
{
return "Hello " + s;
}
}
}