In this blog, we are going to create a “Hello World” ASP.Net Web Service (ASMX) with parameters, and allow a request from cross-origin. Now, here we implement ASP.Net Web Service from Visual Studio 2017. We have created an empty ASP.Net Web Application solution and added “demo.asmx” in solution. Then, added code for “HelloWorld” method with “name” parameter as mention below snippet,
    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public string HelloWorld(string text) { 
        return "Hello World " + text; 
    } 

We will invoke this web service using HTTP verb. So, we defined “ScriptMethodAttribute” for setting response format JSON.

We need to enable script service to invoke & pass parameters in Web Service from the script as shown in the below screenshot:

Then set demo.asmx as the startup page and run/test this web service on IIS:

Below is the example of “HelloWorld” webservice:
 

Now, we will create another ASP.NET web application which will have index.html to request “Hello world” web service using jQuery,
$.ajax({ 
    type: "POST", 
    url: "http://localhost:50555/Demo.asmx/HelloWorld", 
    data: { 
        'text': ' Peter' 
    }, 
    success: function(data) { 
        console.log(data); 
    }, 
    error: function(request, status, error) { 
        console.log(request); 
    } 
}); 


We will receive this error message: “No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:11111' is therefore not allowed access.”

To solve the above error we will need to add the following code under configuration node in web.config,
<system.webServer> 
    <httpProtocol> 
        <customHeaders> 
            <add name="Access-Control-Allow-Headers" value="accept, content-type" /> 
            <add name="Access-Control-Allow-Origin" value="http://localhost:11111" /> 
            <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" /> 
        </customHeaders> 
    </httpProtocol> 
</system.webServer> 

Now, when we request this web service, it will successfully return and the response of the web service is as below in the console:

We have learned about passing data to ASP.NET Web Service (ASMX) from Cross-Origin.

HostForLIFE.eu ASP.NET Core 2.2.1 Hosting
European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.