Zuhaib
Zuhaib

Reputation: 1420

Invoke Remote ASP.NET WebService Using jQuery

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.

Upvotes: 2

Views: 2452

Answers (3)

christian
christian

Reputation: 558

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.

Upvotes: 0

RameshVel
RameshVel

Reputation: 65887

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.

Upvotes: 2

Tzury Bar Yochay
Tzury Bar Yochay

Reputation: 9004

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.

Upvotes: 0

Related Questions