Error consuming webservice in C #

0

I'm doing a web service in C #, to consume its data via javascript (without being an application made in ASP.NET or any .NET technology, I want to use only html and javascript).

My web service has been configured like this:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class PhoneGap : System.Web.Services.WebService
{


    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}

And my ajax looks like this:

function GetData() {
    $.ajax({
        type: 'POST'
        //Caminho do WebService + / + nome do metodo
        , url: "http://localhost:3458/PhoneGap.asmx/HelloWorld"
         , contentType: 'application/json; charset=utf-8'
            , dataType: 'json'
            //Abaixo adicione as variáveis caso haja alguma.
            , data: ''
            , success: function (data, status) {
                //Caso não ocorra nenhum tipo de erro:
                $('.valor').text(data.d);
            }
            , error: function (xmlHttpRequest, status, err) {
                //Caso ocorra algum erro:
                $('.valor').html('Ocorreu um erro');
            }
    });
}

When I try to use the Hello Word of the web service, it is giving an error (Note: I thought I was able to use the local web service for testing but I did not find anything using html and ajax.)

Error:

Failed to load link : Response to preflight request does not pass access control check: No 'Access -Control-Allow-Origin 'header is present on the requested resource. Origin 'null' is therefore not allowed access.

    
asked by anonymous 11.04.2018 / 15:18

1 answer

2

For the call to work from a different source, you must configure Cross-Origin Resource Sharing (CORS). For an ASP.Net project with ASMX, the following section in Web.Config can solve your problem:

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Headers" value="accept, content-type" />
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS" />
     </customHeaders>
</httpProtocol>

However, in order to be able to call the ASMX WebService method from Javascript, the WebService class must contain the [ScriptService] attribute, like this:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService] // AQUI
public class WebService : System.Web.Services.WebService

Only one remark : Is it really necessary to use ASMX for this project? ASMX webservices are a legacy technology and should not be considered in new projects. I recommend giving a search on the ASP.Net Web API or even WCF .

    
11.04.2018 / 16:00