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.

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
0

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.

Comments

0

The best way to consume a remote web service (those who don't exists in the same website domain) is to communicate the request via PHP (or similar server side scripting). In the case of PHP you can follow this gross steps:

  1. your html page hosted on example.com require a remote service hosted in example2.com.

  2. your html page hosted on example.com consume a service via PROXY reading from a PHP script located on the same example.com domain:

    $.ajax({ ..bla..., url: '/phpscripts/proxy.php'});

  3. the /phpscripts/proxy hosted on example.com is who will communicate to example2.com using the CURL php library. It will receive an html response (or something else, depends on the web service) from example2.com and next re-transmit the processed message back to your html page hosted in example.com. you can catch this response in example.com using the ,success(html){ } argument in the $.ajax object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.