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

165
Vistas
Ajax calling 401 unauthorized from backend in html

I have a problem with my AJAX. Using Insomnia, I was able to get in with a successful response of 200 using the API token.

However, when I implement it in the HTML, I get a 401 response of access denied.

$.ajax({
  url: "https://ecoexchange.dscloud.me:8080/api/get",
  method: "GET",
  apikey: sessionStorage.getItem("apikey"),
  dataType: 'json',
  success: function(result) {
    $('#infoTable tr').empty();
    var header = $('#infoTable thead');
    var body = $('#infoTable tbody');
    var hTr;
    $('#infoTable thead').append(hTr = $('<tr>'));
    // Headers
    for (var h = 0; h < result.headers.length; h++) {
      hTr.append($('<th>', {
        text: result.headers[h]
      }))
    }
    // Body
    for (var d in result.data) {
      var data = result.data[d];
      $('#infoTable tbody').append($('<tr>')
        .append($('<td>', {
          text: data.RecyclableID
        }))
        .append($('<td>', {
          text: data.Name
        }))
        .append($('<td>', {
          text: data.RecyclableType
        }))
      )
    }
  }
})

I am not sure how to put in the token or user name or password.

How can I improve the code so I don't get the error?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

What is this apikey parameter you're using? That's not in the documentation.

apikey: sessionStorage.getItem("apikey"),

Did you mean to pass it as a header instead? For example:

headers: {"apikey": sessionStorage.getItem("apikey")},

The documentation for the service you're using should specify how to include the API key. Presumably you have that information, because:

Using Insomnia, I was able to get in with a successful response

So you'll need to include the value in your AJAX request wherever it belongs. Most likely as either a header value or a query string value. But the jQuery .ajax() function isn't going to know how to pass the value, you have to specify.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think your problem is with passing queries which can be solved here

As it's been said on David's answer , You must know where your apikey is required on the server-side, in header or queries(parameters).

If your apikey is required on queries based on the docs you can use :

$.get('/api/get' , {'apikey' : 'YOUR-KEY'}).done((res) => {
     console.log(res) 
})

Or if your apikey is required in headers:

$.ajax({

        url: '/api/get',
        type: 'GET',
        headers: {'apikey' : 'YOUR-KEY'},
        success : (res) =>{
            console.log(res);
        }

})

jQuery.ajax() docs can be found here

And jQuery.get() docs here

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