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

136
Vistas
I need some help getting parameters to go through with my AJAX call

I'm making a call with ajax to my db to get some data. And I try to limit it to a specific Id. And I got the backend working. it works when I visit the specific localhost url or when I call it from Postman. I then get the expected value. However when I try to make a call from my client with AJAX I just don't get anything at all. I got into a thing where sometimes it would work and sometimes it didn't.

const GetUserById = (id) => {
    let url = "/Users/1";
    $.get(url, (gotUser) => {
        alert(gotUser.id);
    })
}

I have this method that I just hardcoded to go to /Users/1 for testing purposes. And when I call this in postman or go to localhost:8080/Users/1 I get the thing I expect. However here It just doesn't go anywhere at all.

app.get("/Users/:id", (request, response) => {
    let sqlCode = "select Id, Username, [Password], Credits from Users Where Id = " + request.params.id;

    sql.query(connString, sqlCode, (error, result) => {

        if (error) {
            console.log(error);
        }
        else {
            response.json(result);
        }
    });
});

The server side if that's interesting.

EDIT: added a try catch like this. it doesn't even get to the catch. It get's to the line that's $.get(url, (gotUser) => { then it goes to the end of the function and doesn't get anything.

 try {
        $.get(url, (gotUser) => {
            alert(gotUser.id);
        })
    } catch (error) {
        console.log(error);
    }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

A typical issue with processing ajax requests are CORS. In your case a quick fix to that would be to specify at the response headers to allow the origins, like that:

response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

In a more general way you might need to specify it in application level, or use any package to deal with that(cors).
EDIT ---

app.get("/Users/:id", (request, response) => {
response.header("Access-Control-Allow-Origin", "*");
response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

    let sqlCode = "select Id, Username, [Password], Credits from Users Where Id = " + request.params.id;

    sql.query(connString, sqlCode, (error, result) => {

        if (error) {
            console.log(error);
        }
        else {
            response.json(result);
        }
    });
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

If it is a cors issue try something along these lines

var cors = require('cors');
    
app.get("/Users/:id",cors(), (request, response) => {
            let sqlCode = "select Id, Username, [Password], Credits from Users Where Id = " + request.params.id;
        
sql.query(connString, sqlCode, (error, result) => {
        
     if (error) {
                    console.log(error);
                }
                else {
                    response.json(result);
                }
            });
        });
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