Calling remote ASP.NET WebService using jQuery

I wrote a simple ASP.NET WebService Precompiled and placed 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 at the same IP, it works as expected. But if I access it from a different IP (ex: 127.0.0.1) then I get "Permission Denied" in IE and "404 object not found" in FireFox.

How to write a web service using ASP.NET that can be used remotely from anywhere and what Javascript needs to be in order to use it.

Every example I have found is for a WebService hosted in the same project directory.

+2


source to share


3 answers


You cannot use web services or any web resources from other domains (without cross domain webservice access). You can only access resources from your domain.



instead, you can use the Dynamic Script tag or JSONP to achieve this.

+2


source


On the machine where you serve this web service, make sure that the web server (IIS) allows remote connections. If I'm not mistaken, the development web server does not allow remote connections, at least by default.



0


source


The best way to use a remote web service (those that don't exist on the same site domain) is to pass the request through PHP (or similar server-side scripts). In the case of PHP, you can follow these rough steps:

  • Your html page hosted at example.com requires a remote service hosted at example2.com.

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

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

  • /phpscripts/proxy

    hosted on example.com will communicate with example2.com using the php CURL library. It will receive a html response (or whatever, depending on the web service) from example2.com and then re-upload the processed message back to your html page hosted at example.com. you can catch this answer at example.com using an argument ,success(html){ }

    on an object $.ajax

    .

0


source







All Articles