-
-
Save richellyitalo/276e79c35602fe52a0516b1ffcf52572 to your computer and use it in GitHub Desktop.
AJAX GET and POST with pure Javascript
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
// Exemplo de requisição GET | |
var ajax = new XMLHttpRequest(); | |
// Seta tipo de requisição e URL com os parâmetros | |
ajax.open("GET", "minha-url-api.com/?name=Henry&lastname=Ford", true); | |
// Envia a requisição | |
ajax.send(); | |
// Cria um evento para receber o retorno. | |
ajax.onreadystatechange = function() { | |
// Caso o state seja 4 e o http.status for 200, é porque a requisiçõe deu certo. | |
if (ajax.readyState == 4 && ajax.status == 200) { | |
var data = ajax.responseText; | |
// Retorno do Ajax | |
console.log(data); | |
} | |
} |
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
// Exemplo de requisição POST | |
var ajax = new XMLHttpRequest(); | |
// Seta tipo de requisição: Post e a URL da API | |
ajax.open("POST", "minha-url-api", true); | |
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | |
// Seta paramêtros da requisição e envia a requisição | |
ajax.send("[email protected]"); | |
// Cria um evento para receber o retorno. | |
ajax.onreadystatechange = function() { | |
// Caso o state seja 4 e o http.status for 200, é porque a requisiçõe deu certo. | |
if (ajax.readyState == 4 && ajax.status == 200) { | |
var data = ajax.responseText; | |
// Retorno do Ajax | |
console.log(data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment