Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Editor de código en vivo: no se puede eliminar el primer carácter (usando XMLHttpRequest y matraz)

Tengo un editor de código en vivo y no puedo eliminar el primer carácter, si selecciono todo el texto y presiono delete o backspace , no hace nada.
¿Que puedo hacer?
HTML:

 <!doctype html> <html> <head> <title>Live Code editor</title> </head> <body> <textarea id="text"></textarea> </body> </html>

JavaScript:

 let text=document.getElementById('text') let xhr=new XMLHttpRequest() function sendRequest(tobeSend,writeResponse) { xhr.open('POST','/files/file.txt',false); if(!tobeSend) { //if `tobeSend` not null xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); } xhr.send(tobeSend); if(writeResponse) { text.value=xhr.responseText; } } sendRequest(null,true) text.addEventListener('input',function(e){sendRequest(text.value,false)}) setInterval(function(){let c={start:text.selectionStart,end:text.selectionEnd};sendRequest(null,true);text.setSelectionRange(c.start,c.end)},200)//fresh the code every 200 millisecond

Python/frasco:

 @app.route('/files/file.txt') def file(): if request.method=='POST': if request.form.text: with open('files/file.txt','w') as f: f.write(str(request.form.text)) with open('files/file.txt') as f: return f.read()

Nota: Lo sé, no debería usar sync xhr, pero cuando envío una solicitud asíncrona y escribo un texto, la función asíncrona que regresa elimina el texto escrito (si alguien conoce una solución asíncrona, realmente lo agradecería).

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Desde entonces he descubierto el problema:
Cuando envío los datos "text=" , request.form.get("text") devuelve '' , y el valor booleano es False .
Así que cambié el código de la siguiente manera:

 @app.route('/files/file.txt') def file(): if request.method=='POST' or request.form.get("text")=='': #intead of `if request.form.get("text")` if request.form.text: with open('files/file.txt','w') as f: f.write(str(request.form.text)) with open('files/file.txt') as f: return f.read()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!