WebService.asmx:
using System;using System.Collections;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Xml.Linq;using System.Data;using System.Configuration;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;/// <summary>///WebService 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService]public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } }
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 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"> <title>无标题页</title> <script type="text/javascript"> function getHello() { WebService.HelloWorld(callComplete); } function callComplete(result) { alert(result); } </script></head><body> <form id="form1" runat="server"> <div> </div> <button onclick="getHello()"> hello world</button> <asp:ScriptManager runat="server" ID="scriptManager"> <services> <asp:servicereference path="WebService.asmx" /> </services></asp:ScriptManager> </form></body></html>
问题点击button的时候,总说WebService 未定义?
------解决方法--------------------------------------------------------
WebService.HelloWorld(callComplete);
你这调用有问题吧 又不是静态的
------解决方法--------------------------------------------------------
不可以这么写,cs文件中就可以了.
------解决方法--------------------------------------------------------
js调用post,get两种调用,当然也是你说的codebehind,还有AJAX控件调用但是这个没有拉控件不行的吧?当然还有动态调用.哈哈