Calling a .net webservice from a plain HTML site using jQuery
- by pwee167
Hi guys,
I wanted to know it is possible to call a hosted .net web service from a HTML page using jQuery? I tried this piece of code, but it doesn't work for me:
$('#myForm').click(function() {
$.ajax({
type: "POST",
data: '{}',
url: "http://localhost:49590/Service.asmx?op=HelloWorld",
contentType: "application/json; charset=utf-8",
dataType: "json",
success:
function(response) {
alert(response.d);
},
failure:
function(result) {
alert(result.status + ' ' + result.statusText);
}
});
});
And the webservice is as such:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 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 HelloWorld()
{
return "Hello World";
}
}
I have searched for this and found only examples where it is done from asp.net or asp.net mvc projects. I am not sure what I am missing but I thought this was possible from a plain HTML site with javascript, so can someone please point me in the right direction.
Cheers!