html:<%@ 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">
        var xmlHttp;
        
        function createXMLHttpRequest()
        {
            if(window.ActiveXObject)
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            else if(window.XMLHttpRequest)
                xmlHttp=new XMLHttpRequest();
        }
        
        function AsynRequest()
        {
            createXMLHttpRequest();
            
            xmlHttp.open("post","http://localhost:1675/WebSite2/Service.asmx/GetServerTime",true);            xmlHttp.onreadystatechange=function()
            {
                if(xmlHttp.readyState==4)
                {
                    if(xmlHttp.status==200)
                    {
                        UpdateClock(xmlHttp.responsesText);
                    }
                }
                
            };
            xmlHttp.send("");
        }
        
        function UpdateClock(aTime)
        {
            document.getElementById("clock").innerHTML=aTime;
        }
        
    </script>
</head>
<body onload="AsynRequest()">
    <form id="form1" runat="server">
    <div>
       <span id="clock"></span>
    </div>
    </form>
</body>
</html>
webservice:using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Script.Services;[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]    [ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () 
    {        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }    [WebMethod]
    public string HelloWorld() 
    {
        return "Hello World";
    }    [WebMethod]
    public string GetServerTime()
    {
        return DateTime.Now.ToString();
    }
}

解决方案 »

  1.   

    调不到啊,怎么回事??总返回 undefined 。
      

  2.   

       <webServices>
          <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
          </protocols>
        </webServices>在web.conifg中加入。
      

  3.   

    完整代码
    http://blog.csdn.net/lynnlin1122/archive/2009/02/05/3864713.aspx
      

  4.   

    为什么 我 直接访问这地址就不能访问呢?
     http://localhost:1675/WebSite2/Service.asmx/GetServerTime
      

  5.   


    老大,我用了第一种get方式也 调不到啊?
      

  6.   

    直接vs启动的时候1675端口会随机变化的。当你重新启动的时候,这样的路径就不对了。要求你换成直接iis里面建立,不要使用vs自己带的服务器,有了固定的路径,直接访问路径。如果没错,就能被调用 。
      

  7.   

    把那个xmlHttp.open("post","http://localhost:1675/WebSite2/Service.asmx/GetServerTime",true)
    改成false看看
      

  8.   

    可我直接访问http://localhost:1675/WebSite2/Service.asmx/GetServerTime 这个地址会出错。
      

  9.   

    没有问题啊,注意返回的是xml
    <!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">
          var xmlHttp;      function createXMLHttpRequest() {
            if (window.ActiveXObject)
              xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            else if (window.XMLHttpRequest)
              xmlHttp = new XMLHttpRequest();
          }      function AsynRequest() {
            createXMLHttpRequest();        xmlHttp.open("GET", "Service.asmx/GetServerTime", true);        xmlHttp.onreadystatechange = function () {
              if (xmlHttp.readyState == 4) {
                if (xmlHttp.status == 200) {
                  UpdateClock(xmlHttp.responseXML.selectNodes("//string")[0].text);
                }
              }        };
            xmlHttp.send("");
          }      function UpdateClock(aTime) {
            document.getElementById("clock").innerHTML = aTime;
          }
            
        </script>
    </head>
    <body onload="AsynRequest()">
        <form id="form1" runat="server">
        <div>
           <span id="clock"></span>
        </div>
        </form>
    </body>
    </html>
      

  10.   

    POST soap格式化串到的地址应该是 http://localhost:1675/WebSite2/Service.asmx,
    GetServerTime 只是ws的一个方法。
    header注意需加上Content-type:text/xml;用用java的soap工具soapui吧,很方便,只需载入wsdl路径(你这个应该是http://localhost:1675/WebSite2/Service.asmx?wsdl),就可自动生成发送的xml+soap串。还可以查看具体的http header和body.
    你将生成的串+参数(如果必要)作为http body发送到http://localhost:1675/WebSite2/Service.asmx既可。
      

  11.   

    本帖最后由 net_lover 于 2010-05-04 16:08:56 编辑
      

  12.   


    为什么我那边就是不行,  webservice 是不是还要配置什么啊?
      

  13.   

    我不注释 调 if (xmlHttp.status == 200)  的话,什么反应都没有。我注释掉这句 就抛出
      

  14.   

    web.config里加
      <system.web>
        <webServices>
          <protocols>
            <add name= "HttpPost" />
            <add name= "HttpGet" />
          </protocols>
        </webServices>
      </system.web>
      

  15.   

    就是跟我直接访问 http://localhost:1675/WebSite2/Service.asmx/GetServerTime 出同个错误。
      

  16.   

    你应该看看(伴水 清洁工)怎么利用js发送soap请求到csdn的webservice的.
    http://topic.csdn.net/u/20100420/13/7a7d589f-82af-49cb-ba9b-481499cd1e82.html
    实质就是利用javascript POST http 头加http soap格式化串body.
      

  17.   

    直接用框架不就完了,jquery的ajax就可以了
      

  18.   


    上面的方法XMLHttpRequest简单,无需构造SoapHead等
      

  19.   

    js调用webservice是需要在页面中引入一个microsoft提供的一个webservice.htc
    你这样的请求,返回只是xml,并非webservice方法执行的结果