0

I have developed a rest api in node js with a get endpoint that, based on a received parameter, returns some data. I'm consuming the api from jquery, but I can't send it that parameter it needs in the get request I make. If I make the request from Postman, it works perfectly for me. This is the endpoint code:

async function getIncidentsByState(req,res){
    const params  = req.body;
    const completed = params.completed;
    const idIncident = params.id;
    console.log(completed);
    try {
        const incidents = await Incident.find({completed:completed}).sort({create_at:1});
        if(!incidents){
            res.status(400).send({msg:"Error al obtener las incidencias"});
        }
        else{
            res.status(200).send(incidents);
        }
    } catch (error) {
        res.status(500).send(error);
    }

}

The jquery code that make the request is this:

$.ajax({
    type: "GET",
    dataType: "json",
    contentType: "application/json",
    data: {
        "completed":false
    },
    url: "http://localhost:3600/api/getIncidentsByState",
    
    success: function(data){
        mostrarIncidencias(data);
    }
});

The parameter is not reaching the api and endpoint is printing undefined.

I've tried dozens of different ways,im desesperated.

I would appreciate suggestions on what is going on.

Thanks in advance

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
user3118887
  • 55
  • 1
  • 15
  • Did you print `req` and see what is there? – kiner_shah Jan 04 '22 at 10:23
  • 3
    Browsers will not send a body with a GET request (and even i they did, you shouldn't set a JSON content-type without also passing some JSON to `data`). Use a query string instead. – Quentin Jan 04 '22 at 10:23

0 Answers0