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

277
Views
sending form data with javascript with XMLHttpRequest asynchronously

I am sending some form data with an XMLHttpRequest

let xhr = new XMLHttpRequest();

xhr.open("POST", url, true);


xhr.setRequestHeader("Content-Type", "application/json");
try {
    let data = JSON.stringify(sendForm);
    xhr.send(data);
    alert(xhr.status);
 }
 catch (error) {
     alert(error);
 }

This works as it will go into this after the alert pops up in the browser

xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
        location.href = 'thanks.html';
    }

and data will be inserted into the database as expected. However, if I comment out the alert after the xhr.send(data); this code will no longer inserts data into the database or display the correct page, it just displays the js file. If I set a break point at xhr.send(data) when I step past it is when the js file is displayed. Any thoughts as to why the data is being sent if I include the alert(xhr.status) step in the code but not when I comment it out? Also, any thoughts why the try{}catch{} is not catching the error? Thanks I am not very familiar with javascript right now.

about 4 years ago · Juan Pablo Isaza
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!