Dar de alta sistmeas en ajax popup

This commit is contained in:
fjrodriguez
2023-02-16 10:38:28 -06:00
parent 3aaa0cd909
commit 0de2d8a735
9 changed files with 401 additions and 68 deletions

View File

@@ -6,7 +6,7 @@
{% block content %}
<form method="POST" action=".">
<form method="POST" action="." id="cliente-form">
{% csrf_token %}
<div class="form-group">
<label for="{{form.RFC.name}}"><strong> {{form.RFC.label|capfirst}} </strong></label>
@@ -34,7 +34,51 @@
{% block scripts %}
<script>
document.addEventListener("DOMContentLoaded", ()=>{
if(window.name ==='popupWin')
{
console.log('popupWin')
document.querySelector("#cliente-form").addEventListener('submit',(event)=>{
event.preventDefault();
let form= event.target;
let url = `{% url 'add_cliente' %}`;
let data = new FormData(form);
let xhr = new XMLHttpRequest();
xhr.open('POST',url);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange = ()=>{
if(xhr.readyState===XMLHttpRequest.DONE){
if(xhr.status===200){
let response = JSON.parse(xhr.responseText);
window.opener.postMessage({data:response, windowName:'clientWindow'},window.location.origin);
window.close();
}else{
console.error('Request error ',xhr.statusText);
}
}
};
xhr.send(data);
/*
fetch(url,{
method:'POST',
body:data
})
.then(response=>response.json())
.then(response=>{
window.opener.postMessage({data:response, windowName:'clientWindow'},window.location.origin);
window.close();
})
.catch(error => console.log(error));
*/
});
}else{
console.log('not popupWin')
}
})
window.addEventListener("load", (event)=>{
if(!id_Activo.checked){
id_fecha_baja.setAttribute('required','')