Skip to content

Instantly share code, notes, and snippets.

@cadu-leite
Last active May 4, 2022 00:30
Show Gist options
  • Save cadu-leite/301289387580e0adc02c159c25b146f9 to your computer and use it in GitHub Desktop.
Save cadu-leite/301289387580e0adc02c159c25b146f9 to your computer and use it in GitHub Desktop.
JavaScript XMLHttpRequest GET and POST forms js - good to go with Django Python
// http://localhost:8080/contact/form/
// oReq.open("GET", "http://localhost:8080/contact/form");
// id_SubmitContact
var container = "header";
var formId = "id-contactform";
var url_post = "/contact/form/"
function callBackLoad(){
formRender(this.responseText);
};
function callBackSubmit(event) {
event.preventDefault();
formPost(event)
}
function formRender (content) {
element = document.getElementById(container);
element.innerHTML = content;
ctctForm = document.getElementById(formId);
ctctForm.addEventListener('submit', callBackSubmit);
};
function formGet(urlGet){
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", callBackLoad);
oReq.open("GET", urlGet);
oReq.send();
};
function formPost(event){
var formPost = new XMLHttpRequest();
var formData = new FormData(ctctForm );
x = formPost.open('POST',url_post, false); // Não assincrono.
y = formPost.send(formData);
console.log('responseText:=>>'+formPost.responseText);
console.log('responseXML:=>>'+formPost.responseXML);
console.log('responseSTATUS:=>>'+formPost.status);
console.log('X=>>'+x);
console.log('Y=>>'+y);
// ctctForm.reset();
formRender(formPost.responseText);
var myModal = new bootstrap.Modal(document.getElementById('myModal'), options)
};
window.onload = function contactFormSet(){
formGet('http://localhost:8080/contact/form/');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment