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

133
Views
usando POST en ajax en vanilla js

Estoy tratando de enviar la solicitud de publicación a mi archivo PHP pero dice índice indefinido.

mi código js -

 document.getElementById("btn1").addEventListener('click', xh ); function xh(){ xhr = new XMLHttpRequest(); //xhr.open('GET', 'req.php?msg=hello bro', true); xhr.open('POST', 'req.php'); xhr.getResponseHeader('Content-type', 'application/w-xxx-form-urlencoded'); xhr.onprogress = function () { // document.getElementById("loading").classList.remove("dis"); } xhr.onload = function () { console.log(this.responseText); } xhr.send("msg=hello"); }

mi código PHP -

 <?php if(isset($_POST['msg'])){ echo "set"; } else{ echo "not set"; } ?>

También probé el método de solicitud del servidor pero no funcionó.

se muestra no configurado, y si trato de hacer eco de $_POST['msg']; dice índice indefinido 'msg'

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

0

En vez de

 xhr.getResponseHeader('Content-type', 'application/w-xxx-form-urlencoded');

desea establecer el encabezado de la solicitud

 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

Leer más sobreXHR . Sin embargo, el enfoque actual es usar fetch() .

 fetch("req.php", { method: "POST", headers: {'Content-Type': 'application/x-www-form-urlencoded'}, body: "msg=hello" }).then(response => response.text()) .then(console.log)
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!