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

170
Views
How to pass data to JavaScript ajax xmlhttprequest
function fetchJSONFile(isRest, path, callback) {
  var httpRequest = new XMLHttpRequest();
  httpRequest.onreadystatechange = function() {
    if (httpRequest.readyState === 4) {
      if (httpRequest.status === 200) {
        var data = JSON.parse(httpRequest.responseText);
        if (callback) callback(data);
      }
    }
  };

  httpRequest.open('POST', path, true);
  if (isRest) {
    let mail = "***";
    let pw = "***";
    httpRequest.setRequestHeader("Authorization", "Basic " + window.btoa(mail + ":" + pw));
  }
  httpRequest.send("--data '[1, 2]'"); // also tried data=[1, 2]
}

I want to pass my data to the request, so that i'm able to get a response with the data i need. Currently i'm getting 415 error. What's wrong with my code?

Curl for api:

curl --user test@gmail.com:secret -X POST -H 'Content-Type: application/json' \
    http://localhost:8888/api/quizzes/1/solve --data '[1, 2]'
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

According the curl, you need to also set as header the Content-Type, it would look something like this:

httpRequest.setRequestHeader('Content-Type', 'application/json');

and then send with JSON.stringify method to make sure you send JSON in proper string format.

httpRequest.send(JSON.stringify({ myKey: 'myValue' });
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!