- Para quem está tendo dificuldade com requisições AJAX para coletar alguns dados da API com método POST ou GET.
- Documentação AJAX: https://www.w3schools.com/js/js_ajax_intro.asp
- Codigo GitHub: https://github.com/lipeomiguel/AluraTools/blob/main/Doc/js/Tools/Ajax/ajaxResquest.js
function xmlRequest(method='GET',url='https://api.ipify.org?format=json',asyncCallback=true,callback=false){
this.method = method;
this.url = url;
this.asyncCallback = asyncCallback;
this.callback = callback;
this.return = (callback)=>{
/**
* ! Check if the callback is being called!
* ? ajax.return(?);
**/
const xml = new XMLHttpRequest();
xml.open(this.method,this.url,this.asyncCallback);
xml.send();
xml.onreadystatechange = ()=>{
if(xml.readyState == 4){
callback(xml);
}
}
}
}
const ajax = new xmlRequest();
ajax.return(dados); //? Create callback for data processing: [ ajax.return(a); ]
function dados(xml){
console.log(xml.responseText);
}
"JS AJAX RESQUEST [ GET | POST ]": {
"prefix": "JSAJAXRESQUEST",
"body": [
"function xmlRequest(method='GET',url='https://api.ipify.org?format=json',asyncCallback=true,callback=false){",
" this.method = method;",
" this.url = url;",
" this.asyncCallback = asyncCallback;",
" this.callback = callback;",
" this.return = (callback)=>{",
" /**",
" * ! Check if the callback is being called!",
" * ? ajax.return(?);",
" **/",
" const xml = new XMLHttpRequest();",
" xml.open(this.method,this.url,this.asyncCallback);",
" xml.send();",
" xml.onreadystatechange = ()=>{",
" if(xml.readyState == 4){",
" callback(xml);",
" }",
" }",
" }",
"}",
"const ajax = new xmlRequest();",
"ajax.return($1); //? Create callback for data processing: [ ajax.return(a); ] ",
],
"description": "This AJAX function is used to make simple requests like GET or POST.",
}
3. ATENÇÃO: SOLICITAÇÕES COM ENVIO DE PARÂMETROS DE HEADER, É NECESSÁRIO TER A DOCUMENTAÇÃO E FAZER ALTERAÇÕES NO CÓDIGO.