I just cannot get this to work! Whenever I click the button nothing happens. Have replaced the .load method with .text(‘hello world’) and it works so seems like my problem is in the url of the method. Have looked everywhere for an answer but think I must be overlooking something really obvious as have not seen the same thing posted anywhere. Please help its driving me crazy!!!
Client-Side:
<script src="Scripts/jQuery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
// Cannot get this to work
$(function () {
$('#buttonSays').click(function () {
$('div').load('AjaxServices.asmx/HelloWorld');
});
});
</script>
</head>
<body>
<h1>Hello World from Web Service</h1>
<button type="button" id="buttonSays">Get Info</button>
<p>The site says...</p>
<div id="divSays1"></div>
<div id="divSays2"></div>
<div id="divSays3"></div>
</body>
Server-Side:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AjaxServices : System.Web.Services.WebService
{
private static int count = 0;
[WebMethod]
public string HelloWorld()
{
count++;
return "Hello World #" + count.ToString();
}
While this may not be what’s causing your problem, a common issue that prevents .Net web services from working with jQuery is that
GETandPOSTrequests are not allowed by default. Make sure that you have enabled the appropriate method(s) in your web.config using the code below:I believe you could also specify which method(s) are allowed at the WebMethod level: