I have two projects:
ASP.net web site project:
Default.aspx
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type = "text/javascript">
function DisplayMessageCall() {
$.ajax({
type: "POST",
url: "http://localhost:24218/Service1.asmx/HelloWorld",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccessCall,
error: OnErrorCall
});
}
function OnSuccessCall(response) {
$('#<%=lblOutput.ClientID%>').html(response.d);
}
function OnErrorCall(response) {
alert(response.status + " " + response.statusText);
}
</script>
<h2>Example 1: Call Webservice using JQuery AJax (Without Input)</h2>
<asp:Button ID="btnGetMsg" runat="server" Text="Click Me" OnClientClick="DisplayMessageCall();return false;" /><br />
<asp:Label ID="lblOutput" runat="server" Text=""></asp:Label>
And ASP Web Service Application:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "Hello World";
}
}
I added in my ASP web site the reference for my webService but when I click on the button I get the error message. When I enter the url
(http://localhost:24218/Service1.asmx/HelloWorld)
in the browser I get the following result:
<string xmlns="http://tempuri.org/">Hello World</string>
I use ASP.NET 4.5.