Created
March 18, 2018 11:27
-
-
Save jonataa/1313452c4f2d665a3bc90f8ca989d985 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>XHR</title> | |
</head> | |
<body> | |
<input type="text" id="input-cep"> | |
<button onclick="buscarCep('#input-cep')">Buscar CEP</button> | |
<p>Isso esta vazio!</p> | |
<script> | |
function buscarCep(inputId) { | |
var input = document.querySelector(inputId); | |
var cep = input.value; | |
function responseSuccess () { | |
var resultado = document.querySelector('p'); | |
resultado.innerHTML = this.response.cidade; | |
} | |
if (cep !== '') { | |
var oReq = new XMLHttpRequest(); | |
oReq.onload = responseSuccess; | |
oReq.open("get", "http://api.postmon.com.br/v1/cep/" + cep, true); | |
oReq.responseType = "json"; | |
oReq.send(); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment