AJAX Web服务调用的问题:Microsoft JScript运行时错误:Sys.Net.WebServiceFailedException:

时间:2009-03-21 12:03:20

标签: .net ajax web-services

我正在尝试从同一台机器[Win XP Home]中的客户端网站调用AJAX Web服务调用。这样做,我收到以下错误。

Microsoft JScript runtime error: Sys.Net.WebServiceFailedException: The server method 'Greetings' failed with the following error: <html>
    <head>
        <title>Not Found</title>
        <style>
            body {font-family:"Verdana";font-weight:normal;font-size: 8pt;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: 8pt}
            .marker {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>Server Error in '/WebServiceClient1' Application.<hr width=100% size=1 color=silver></h1>

            <h2> <i>HTTP Error 404 - Not Found.</i> </h2></span>

            <hr width=100% size=1 color=silver>

            <b>Version Information:</b>&nbsp;ASP.NET Development Server 9.0.0.0

            </font>

    </body>
</html>

以下是服务器和客户端代码

服务器代码:

[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 Service : System.Web.Services.WebService
    {

        [WebMethod]
        public string Greetings(string name)
        {
            string msg;
            msg = String.Format("Hello {0}: Processed on {1}", name, System.DateTime.Now.ToLongTimeString());
            return msg;
        }
    }

客户端代码:

<script type ="text/javascript">
    function GreetingsButtonOnClick() {
        Service.Greetings($get("NameTextBox").value, OnGreetingsComplete);
    }  //error occurs at this line and the control goes to scriptresource.axd file

    function OnGreetingsComplete(result) {
        var elem = $get("Results");
        elem.innerHTML = result;
    }
</script>

    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="http://localhost:4033/WebService1_AJAX/Service.asmx" />
        </Services>
    </asp:ScriptManager>

(请注意,在服务器内调用Web服务函数调用工作正常并且我已正确定义了&amp;服务标记)

js file (http://localhost:4033/WebService1_AJAX/Service.asmx/js)
--------
var Service=function() {
Service.initializeBase(this);
this._timeout = 0;
this._userContext = null;
this._succeeded = null;
this._failed = null;
}
Service.prototype={
_get_path:function() {
 var p = this.get_path();
 if (p) return p;
 else return Service._staticInstance.get_path();},
Greetings:function(name,succeededCallback, failedCallback, userContext) {
return this._invoke(this._get_path(), 'Greetings',false,{name:name},succeededCallback,failedCallback,userContext); }}
Service.registerClass('Service',Sys.Net.WebServiceProxy);
Service._staticInstance = new Service();
Service.set_path = function(value) { Service._staticInstance.set_path(value); }
Service.get_path = function() { return Service._staticInstance.get_path(); }
Service.set_timeout = function(value) { Service._staticInstance.set_timeout(value); }
Service.get_timeout = function() { return Service._staticInstance.get_timeout(); }
Service.set_defaultUserContext = function(value) { Service._staticInstance.set_defaultUserContext(value); }
Service.get_defaultUserContext = function() { return Service._staticInstance.get_defaultUserContext(); }
Service.set_defaultSucceededCallback = function(value) { Service._staticInstance.set_defaultSucceededCallback(value); }
Service.get_defaultSucceededCallback = function() { return Service._staticInstance.get_defaultSucceededCallback(); }
Service.set_defaultFailedCallback = function(value) { Service._staticInstance.set_defaultFailedCallback(value); }
Service.get_defaultFailedCallback = function() { return Service._staticInstance.get_defaultFailedCallback(); }
Service.set_path("/WebService1_AJAX/Service.asmx");
Service.Greetings= function(name,onSuccess,onFailed,userContext) {Service._staticInstance.Greetings(name,onSuccess,onFailed,userContext); }

2 个答案:

答案 0 :(得分:1)

Web服务是不同的项目吗?如果是这样,.NET会认为它是跨域调用(我认为因为客户端和服务在开发人员Web服务器的不同实例中运行),ASP.NET AJAX不支持。

简单的解决方案是将服务移动到同一个项目和使用它的客户端。

答案 1 :(得分:0)

您必须使用Method=POST。例如:

WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
          Method = "POST",
          RequestFormat = WebMessageFormat.Json,
          ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
public string DoMyWork()
{

}