views:

391

answers:

2

I wrote a simple ASP.NET WebService Precompiled it and hosted it in a virtual directory.

Webservice code:

namespace Test.Services
{
    /// <summary>
    /// Summary description for AgentService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class TestService : System.Web.Services.WebService
    {
     [WebMethod]
     [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
     public string SayHello(string name)
     {
      return "Hello, " + name;
     }
    }
}

Javascript code to access this webservice:

function SayHello() {
    var serviceURL = "http://172.42.100.47/TestService/TestService.asmx/SayHello";
    var requestData = "{name:'" + $('#txtName').val() + "'}";

    $.ajax({
     type: 'POST',
     contentType: 'application/json; charset=utf-8',
     url: serviceURL,
     data: requestData,
     dataType: 'json',
     success: function(result){
      alert(result.d);
     }
    });
}

If i use this function from a page hosted in the same IP then it works as expected. But if I access it from a different IP (eg: 127.0.0.1) then I get "Permission Denied" in IE and "404 Object Not Found" in FireFox.

How do I write a webservice using ASP.NET that can be used remotely from anywhere and what should be the Javascript to consume it.

Every example that I found referred to a WebService hosted in the same project directory.

A: 

On the machine where you serve this web service make sure the web server (IIS) allows remote connections. If I am not mistaken, the development web server is not allowing remote connections, at least by default.

Tzury Bar Yochay
+1  A: 

Hi

You cannot consume the webservices or any web resources from other domains (cant access cross domain webservice). You can only access the resources from your domain alone.

instead you could use Dynamic Script Tag or JSONP hack technique to achieve this one.

Cheers

Ramesh Vel

Ramesh Vel
For this to work the data returned from the service should be in JSON. My web service returns xml.I wrote a HttpHandler and then wrote the JSON to the Response .. and it worked.
Zuhaib
yeah, that should work... :)
Ramesh Vel