using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService : System.Web.Services.WebService {    public WebService () {        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }    [WebMethod]
    public string HelloWorld(string name) {
        
        return "Hello World:" + name;
    }    
}
<script  type="text/javascript">    window.onload = function() {        //debugger;
        var data = null;
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        //Webservice location.
        var url = "http://localhost/BabyAssess/WebService.asmx/HelloWorld?name=zhangsan";
        xmlhttp.Open("GET", url, false);
        //下边这三行语句,反过来掉过去注释,反正就是不行。
        xmlhttp.SetRequestHeader("Content-Type", "text/xml; charset=utf-8");
        //xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');   
        //xmlhttp.SetRequestHeader("SOAPAction", "http://tempuri.org/HelloWorld");
        xmlhttp.Send(data);
        var result = xmlhttp.status;
        //OK
        if (result == 200) {
            document.write(xmlhttp.responseText);
        }
        //总是输出500,服务器错误?只是调用本机的web服务呀。而且也没sql语句。
        document.write(xmlhttp.status);
        xmlhttp = null;    }
</script>

解决方案 »

  1.   

    忘记说明一点,不能用微软的htc组件。纯爷们用纯js来搞定的方式。谢谢
      

  2.   

    对了,这个http://localhost/BabyAssess/WebService.asmx是没问题的,可以打开滴
      

  3.   

    xmlhttp.responseText:<html>
        <head>
            <title>因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。</title>
            <style>
             body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
             p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
             b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
             H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
             H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
             pre {font-family:"Lucida Console";font-size: .9em}
             .er {font-weight: bold; color: black;text-decoration: none;}
             .version {color: gray;}
             .error {margin-bottom: 10px;}
             .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
            </style>
        </head>    <body bgcolor="white">            <span><H1>“/BabyAssess”应用程序中的服务器错误。<hr width=100% size=1 color=silver></H1>            <h2> <i>因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。</i> </h2></span>            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">            <b> 说明: </b>执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。            <br><br>            <b> 异常详细信息: </b>System.InvalidOperationException: 因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。<br><br>            <b>源错误:</b> <br><br>            <table width=100% bgcolor="#ffffcc">
                   <tr>
                      <td>
                          <code>执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。</code>                  </td>
                   </tr>
                </table>            <br>            <b>堆栈跟踪:</b> <br><br>            <table width=100% bgcolor="#ffffcc">
                   <tr>
                      <td>
                          <code><pre>[InvalidOperationException: 因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。]
       System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +405881
       System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212
       System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47
       System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +193
       System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155
    </pre></code>                  </td>
                   </tr>
                </table>            <br>            <hr width=100% size=1 color=silver>            <b>版本信息:</b>&nbsp;Microsoft .NET Framework 版本:2.0.50727.3053; ASP.NET 版本:2.0.50727.3053            </font>    </body>
    </html>
    <!-- 
    [InvalidOperationException]: 因 URL 意外地以“/HelloWorld”结束,请求格式无法识别。
       在 System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response)
       在 System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath)
       在 System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
       在 System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
       在 System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
    -->
      

  4.   

    在WebService的System.Web节点下加上
    <webServices> 
    <protocols> 
    <add name= "HttpPost " /> 
    <add name= "HttpGet " /> 
    </protocols> 
    </webServices>
    试试
      

  5.   

      public string HelloWorld(string name) {
            
            return "Hello World:" + name;
        }  
    楼主你的参数name  在什么地方得到的?
      

  6.   

    1、首先通过下面的方法把Webservice在前台引用进来   <asp:ScriptManager runat="server">   <Services>   <asp:ServiceReference Path="WebService.asmx" InlineScript="True" />   </Services>   </asp:ScriptManager>   2、然后就可以通过JS程序进行调用,示例如下:   <script type="text/jscript">   function a(name)   {   WebService.HelloWorld(name,onresult);   } 
      function onresult(date)
        {
              alert(date);
        } 
      </script> 
      

  7.   

    如果是VS2008下运行的话,地址要是这样的才行,http://localhost:2645/WebS/WebService.asmx;加上端口。如果是IIS下运行的,就不要了反正指向asmx这个地址要正确
      

  8.   

    垃圾,你的代码在htm页面能用嘛,太傻咯
      

  9.   

     [System.Web.Script.Services.ScriptService]
    取消注释 要不不能调用