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