Web service in javascript

Asked

Viewed 3,055 times

3

How to consume a Web Service by javascript using Visual Studio?

I did a search and found a way to perform with Jquery but never used this before.

I need to consume this Web Service to search for information like Addresses etc. I already have the same.

  • It is very comprehensive the question, has many ways. The ideal I think would be to work with REST protocol, which is possible to use JSON to consume the service of your server through simple XHR requests.

3 answers

7

function ChamaMetodoDoWebService() 
{              
    try 
    {    
       $.ajax({
         type: "POST",
         url: "http://webserviceURL.asmx/nomeDoMetodo",
         data: "{'parametro: valor'}", // somente se o método exigir parâmetros se não é so deixar 'data: "{}"'
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function(msg) { 
               // seu código quando o retorno for sucesso
               alert(msg.d);
         },
         failure: function(msg) { 
               // seu código quando falhar
               alert('Erro!');
         }
      });
    }
    catch (e)
    {
        alert('Ocorreu um erro ao tentar chamar o método do WebService, erro encontrado: ' + e);
    }
}

More details here and here.

  • Dude, I tried to use this function but I found out that the webservice is WSDL. http://www.byjg.com.br/site/webservice.php/ws/cep?WSDL Otherwise?

  • @Will guy I’m trying to consume here this service of byjg only that ta difficult rs, complicated that when I try to send a soapEnvelope in the POST and contenttype xml the browser changes the POST for OPTIONS because of the cross domain, this problem here. You can only do it by javascript even?

  • Because it is expensive, I stopped with this Web Service ai, I went to an alternative option I am using this one here: cep.republicavirtual.com.br in this case using json already solves the problem, I will post the code here in the answer.

  • @Will saw that you change Webservice, now with the callback it’s really going to work

  • thanks for the help.

3


I found this, maybe it helps you with the WSDL: http://www.ibm.com/developerworks/webservices/library/ws-wsajax/

Use the method ajax jquery.

$.ajax({
  url: "url-do-webservice"});

And use one of the return callbacks to get the answer from it. Example:

$.ajax({
  url: "url-do-webservice",
  success: function (data) { /* data contém o que foi retornado pelo webservice */; }
 });

1

Stayed like this:

    function consultacep() {
        cep = DSCEP.GetText()
        cep = cep.replace(/\D/g, "")
        url = "http://cep.republicavirtual.com.br/web_cep.php?cep=" + cep + "&formato=jsonp&callback=correiocontrolcep"
        s = document.createElement('script')
        s.setAttribute('charset', 'utf-8')
        s.src = url
        document.querySelector('head').appendChild(s)
    }

    function correiocontrolcep(valor) {
        if (valor.erro == 'undefined') {
            lblMensagem.SetText('CEP não encontrado');
            return;
        };
        DSENDERECO.SetText(valor.logradouro)
        DSBAIRRO.SetText(valor.bairro)
        DSCIDADE.SetText(valor.cidade)
        CDESTADO.SetValue(valor.uf)
    }

Browser other questions tagged

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