Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

223
Visualizações
I have two JQuery Ajax API calls to a REST API, one doesn't work why?

I'm doing a get REST api request to a server. The server houses automation jobs what I'm looking to kick off with parameters.

var jobURL = "https://hostname/api/v1/tasks"; 

$.ajax({
    url: jobURL,
    method: 'GET',
    async: false,
    contentType: "application/json",
    data: {
        name: '*JOBNAME*'
    },
    headers: {
        'Authorization': 'Bearer '+ obj.access_token,
        'Content-type': 'application/x-www-form-urlencoded',
        Accept:'application/json'
    },
    success: function(data){
        taskname_obj = data    // result
}

I'm doing this per the documentation of the server product and it is working.

I have a successful request, with items returned in the 'taskname_obj' variable. With that said I'm trying to use the same sort of code for another call.

// THIS IS WORKING TO JOB WITHOUT PARAMS.

        // Let's run the job. 
        $.ajax({
            url: jobURL,
            method: 'POST',
            async: false,
            headers: {
                'Authorization': 'Bearer '+ obj.access_token
            },
            success: function(data){
                taskname_obj2 = data    // result
            }
        })

SO I'm doing the above code

Again I am success, I can start my job. I have indication of success returned into taskname_obj2.

With all that said, I am trying to start this with parameters. I suppose I have to put that into the "data:" item, and I shout use JSON. So... I do the following...

var payload = { 
    USER_ID: "",
    BEGDATE: "060121",
    ENDDATE: "121521"
};

$.ajax({
    url: jobURL,
    method: 'POST',
    async: false,
    contentType: 'application/json',
    data: payload,
    headers: {
        'Authorization': 'Bearer '+ obj.access_token,
        'Content-type': 'application/x-www-form-urlencoded',
        Accept:'application/json'
    },
    success: function(data){
        taskname_obj2 = data    // result
    }
})

This doesn't work. I am getting a 415 error back from the server for unsupported media type. I also tried not using JSON.stringify for the data item but I also get a 415 with that.

I think I'm not telling it correctly how to read my JSON. Can anybody help me understand what I'm doing wrong?

I also tried adding this contentType: 'application/json', to no avail.

UPDATE: Using BARMARs technique returns me a 422 error.

$.ajax({
    url: jobURL,
    method: 'POST',
    async: false,
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify(payload),
    headers: {
        'Authorization': 'Bearer '+ obj.access_token
    },
    success: function(data){
        taskname_obj2 = data    // result
    }
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Get rid of the Content-type and Accept headers. Use the contentType: and dataType: options to specify these.

And since the API requires JSON input, it should be contentType: "application/json"

$.ajax({
    url: jobURL,
    method: 'POST',
    async: false,
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify(payload),
    headers: {
        'Authorization': 'Bearer '+ obj.access_token
    },
    success: function(data){
        taskname_obj2 = data    // result
    }
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda