Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda