0

I'm struggling with my first WebService, so I decided to strip everything down to the basics. Still not working.

I created a brand new ASP.NET Website project.

I added a folder named WebService. In the WebService folder, I added a new WebService using the ASP.NET template. I modified the template only to uncomment the [System.Web.Script.Services.ScriptService].

In the Default.aspx page, after the "Welcome to ASP.NET", I added a panel as follows:

<asp:Panel ID="Panel1" runat="server" Height="200px" Width="200px" BackColor="Blue" onclick="HelloWorld();">
</asp:Panel>

I also added the following scripts to the HeaderContent area:

<script type="text/javascript" language="Javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<script type="text/javascript">

    function OnSuccessCall(response) {
        alert("Success! : " + response);
    }
    function OnErrorCall(response) {
        alert(response.status + " " + response.statusText);
    }
    function HelloWorld() {
        var pageUrl = '<%=ResolveUrl("~/WebService/WebService.asmx")%>'

        $.ajax({
            url: pageUrl + "/HelloWorld",
            success: OnSuccessCall,
            error: OnErrorCall
        });
    }

 </script>

When I run this code, I get Error 500. What am I missing?

//------------------------------- UPDATE ----------------------------------------------

Ahh! A new clue! If move the WebMethod into the code-behind for Default.aspx, not as part of a WebService class but just as a method with a [WebMethod] attribute, it works. (At least I get to my OnSuccess function OK). However, if I then add

contentType: "application/json; charset=utf-8",
dataType: "json",

to the $.ajax() call, I'm back to getting my Error 500 (although it might be for a different reason). Is there some install piece that I'm missing for JSON or something?

//------------------------------- UPDATE ---------------------------------------------- Here's the actual WebService. As I stated before, its exactly what you get when you create a New Item and select Web Service.

If someone can just tell me that this works for them, then I can focus on environment issues.

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( ) {
        return "Hello World";
    }

}
Steve Wash
  • 986
  • 4
  • 23
  • 50
  • it means you need to debug `<%=ResolveUrl("~/WebService/WebService.asmx")%>` – karthikr Apr 11 '13 at 02:19
  • That line produces the following output: var pageUrl = '/WebSite3/WebService/WebService.asmx'. That file is there, and if I rename the file I get a 404 Error, not a 500 Error. – Steve Wash Apr 11 '13 at 03:36
  • WebService.asmx is having syntax error debug it – Arun Killu Apr 11 '13 at 04:05
  • WebService.asmx is exactly the way it comes from the template except that I uncommented the [System.Web.Script.Services.ScriptService]. Other then that, it's the original HelloWorld, nothing else. – Steve Wash Apr 11 '13 at 04:34
  • Can you post code for WebService.asmx? – nmenego Apr 11 '13 at 05:45
  • When you have 500 error, what response text says? it usually has stacktrace and explanation of what happened. Post it here please. – vittore Apr 11 '13 at 12:01
  • your constructor parameter has no name, only a type – JP Hellemons Apr 11 '13 at 12:05
  • Request format is unrecognized for URL unexpectedly ending in '/HelloWorld' InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/HelloWorld'.] System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +569481 System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212 – Steve Wash Apr 11 '13 at 14:09
  • 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) +203 System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +128 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184 – Steve Wash Apr 11 '13 at 14:10
  • THANK YOU. Your suggestion to look at the responseText led me to http://stackoverflow.com/questions/657313/request-format-is-unrecognized-for-url-unexpectedly-ending-in – Steve Wash Apr 11 '13 at 14:18

1 Answers1

1

You either didnt try to compile your code or didnt put your actual code here

[WebMethod]
public string HelloWorld(string name) { //<--- you forget method param name to specify
    return "Hello World";
}

So either remove parameters ( ie public string HelloWorld()) or add name as I showed in your code.

Also as you do not have any parameters you can just put url of your service method in the browser and see what happens. ie go to http://localhost/yourapp/WebService/WebService.asmx/HelloWorld, or whatever real URL is

vittore
  • 17,449
  • 6
  • 44
  • 82
  • Ok, good find on the (string) bad parameter. I'm not sure how that got there, I must have been trying something and got interrupted. However, it wasn't the problem. I remove it and I still get the Error 500. Good news is that I also tried what you suggested and just opened the service in a browser. It works fine, so whatever the problem is, it has something to do with calling the service. To recap: I know it it finds the service because I get a 500 not a 404 (which is what I get when it can't find it) and I know the service itself works because I can run it in a page. – Steve Wash Apr 11 '13 at 13:20
  • Credit to vittore for having me both run the service in a browser AND check the response text for the stack trace. That led me to http://stackoverflow.com/questions/657313/request-format-is-unrecognized-for-url-unexpectedly-ending-in – Steve Wash Apr 11 '13 at 14:20
  • @SteveWash that problem with http verbs is quite annoying, I run into similar issue trying to enable PATCH and DELETE verbs to work. – vittore Apr 11 '13 at 16:25