Created
March 18, 2018 13:15
-
-
Save jonataa/90edd9b78e628dc607299426d6957a10 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-longurl"> | |
<button onclick="encurtarUrl('#input-longurl')">Encurtar URL</button> | |
<h2>Sua URL curta é: <a id="url-curta"></a></h2> | |
<script> | |
function encurtarUrl(inputId) { | |
var input = document.querySelector(inputId); | |
var params = { longUrl: input.value }; | |
function responseSuccess () { | |
var urlCurta = this.response.id; | |
var link = document.querySelector('#url-curta'); | |
link.href = urlCurta; | |
link.textContent = urlCurta; | |
} | |
var xhr = new XMLHttpRequest(); | |
xhr.onload = responseSuccess; | |
xhr.onabort = function() { | |
console.log('ERRORR'); | |
}; | |
xhr.open('post', 'https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyDZP-G0GoP3RpJTLc-IKrdZNQEIalduAkY', true); | |
xhr.setRequestHeader('Content-Type', 'application/json'); | |
xhr.responseType = 'json'; | |
xhr.send(JSON.stringify(params)); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment