$.getScript Jquery Javascript replacement

Asked

Viewed 237 times

1

I found a zip code query on the internet, but would not like to use jQuery. It is possible to change $.getScript jQuery for Javascript?

var cepDestino = document.getElementById( 'cepDestino' ).value;
if(cepDestino.trim() != ""){
        $.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep="+cepDestino, function(){

            if (resultadoCEP["tipo_logradouro"] != '') {
                if (resultadoCEP["resultado"]) {
                    document.getElementById( 'bairro' ).value = unescape(resultadoCEP["bairro"]);
                    document.getElementById( 'cidade' ).value = unescape(resultadoCEP["cidade"]);
                    document.getElementById( 'estado' ).value = unescape(resultadoCEP["estado"]);
                }
            }       
        });
    }

NEW CODE

http://jsfiddle.net/dg5bdxyh/

1 answer

1

Using Javascript pure yes, by means of a requisition Ajax.

Returns a json.

var request = new XMLHttpRequest();
request.open('GET', 'http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep='+cepDestino, true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    // Deu certo... Use a variável abaixo para obter os dados
    var data = JSON.parse(request.responseText);
  } else {
    // Não deu certo    
  }
};

request.onerror = function() {
  // A conexão nem sequer deu certo
};

request.send();

To return the fields in certain places do:

document.getElementById('bairro').value(data.bairro)

To see how you are returning the field names, put after the var data, within the if one console.log(data). Make the request and see on console Chrome Inspect Element.

Then you always use: data.nome_do_campo.

  • 1

    Ai for me to show for example the name of bairro in the DIV ID neighborhood, how to do?

  • I updated the Post.

  • That mistake appeared MLHttpRequest cannot load http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep=%22+cepDestino. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.meusite.com.br' is therefore not allowed access.

  • My code was wrong on LINK. Look again, it had an extra quote... on request.open.

  • When I click this message appears Unexpected end of input and when I click the button <input class="button" type="button" id="calcular" value="Calcular" onclick="calc();"> Console message calc is not defined

  • Put your new code in your POST for us to see.

  • Blz, I’m gonna put.

  • See http://jsfiddle.net/dg5bdxyh/

  • You cannot do this test on Jsfiddle, do it on Localhost.

  • I’m doing it directly on my website, I put it on Jsfiddle to send you the code.

  • So there’s a } missing at the end of the function calc the one you passed on to me at Jsfiddle.

  • Yeah, I just found...rsrs. O Access-Control-Allow-Origin, continues.

  • No Stack has several questions about what is happening to you. I believe to be another subject... would extend much here.

  • I am so far trying to find a solution, but without success. You have much more knowledge than I kindly help me.

Show 9 more comments

Browser other questions tagged

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