views:

1807

answers:

3

I am trying to invoke an AJAX web service call from a client web site in the same machine[Win XP Home]. In doing so, I am getting the following error.

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>

Below is the server and client code

server code:

[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;
        }
    }

client code:

<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>

(Note that invoking the web service function call within the server works fine & i have defined the & service tags properly)

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); }
A: 

Is the Web service is a different project? If so, .NET will consider it to be a cross-domain call (I think because the client and service are running in separate instances of the developer web server) which is not supported by ASP.NET AJAX.

The simple solution is to move the service into the same project and the client that consumes it.

Rob Windsor
Rob, I tried to do the same way as you had described but not much luck. It is C# code of ur prsntn. I would like to know how to run the web service and client simultaneouly within the same project? I ran the service in project and the client thru another browser. But it gave the same error again.
A: 

Could you debug your project and browse to http://localhost:4033/WebService1_AJAX/Service.asmx/js ... IE will ask you to save the file, save it as a .js file and open it up. This is the JavaScript the Web Service creates for you. It's usually pretty short, so could you post it here?

Also, is there any reason you're using the full path of the .asmx file? Why not just "~/WebService1_AJAX/Service.asmx"

Cory Larson
Cory, Since the file exceeds the space limit of this comment section, I am pasting the js file in my question itself. Also, if I am not giving the full path and do it the way as you say, the 'Service' class is not recognized. It errors out as 'Error: Service is undefined'
A: 

You have to use Method=POST.

Example:

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

}

Anjum Rizwi

Anjum Rizwi