Error while consuming webservice in c#

Asked

Viewed 416 times

0

I am making a web service in c# , to consume its data via javascript (without being an application made in Asp.net or any technology . NET ,I want to use html and javascript only).

My web service was 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 was 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 am going to take the test to consume the web service Hello Word is giving error(Note: Ententei thinks if I could use the web service locally mind for testing but I did not find anything using html and ajax apeans)

Error:

Failed to load http://localhost:3458/Phonegap.asmx/Helloworld: Response to preflight request doesn’t pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'null' is therefore not allowed access.

1 answer

2


In order for the call to work from a different source, you must set up Cross-Origin Resource Sharing (CORS). For an ASP.Net project with ASMX, the following section on the 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, thus:

[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 observing: 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 a search on ASP.Net Web API or even WCF.

  • It worked , thank you very much, I really don’t think it needs to be ASMX ,as I’m researching ways to search my data that are in my application made in ASP.NET ,I haven’t decided if it will really be this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.