Question :
How to consume a Web Service by javascript using Visual Studio?
I’ve searched and found a way to do it with Jquery but I’ve never used it before.
I need to consume this Web Service to search for information such as Addresses etc. I already have the same.
Answer :
I found this, maybe it helps with WSDL: link
Use the ajax
method of jquery.
$.ajax({
url: "url-do-webservice"});
And use one of the return callbacks to get its response. Example:
$.ajax({
url: "url-do-webservice",
success: function (data) { /* data contém o que foi retornado pelo webservice */; }
});
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);
}
}
It looks 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)
}