Calling a .net webservice from a plain HTML site using jQuery

Posted by pwee167 on Stack Overflow See other posts from Stack Overflow or by pwee167
Published on 2010-04-29T23:38:04Z Indexed on 2010/04/29 23:47 UTC
Read the original article Hit count: 371

Filed under:
|
|

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!

© Stack Overflow or respective owner

Related posts about .NET

Related posts about webservice