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

227
Visualizações
JQuery AJAX URL Rest path parameters

I have a Rest service on /users/{userId}/orders/{orderId} (please note the path parameters) and I want to call it from JQuery.

I could do it simply concatenating the ids like so:

$.get(
    'users/' + 1234 + '/orders/' + 9876,
    function (data) {
        //do something with the response
    }
);

But this doesn't seem like a very correct way of doing it (although it does the job).

Is there a better way of passing these path parameters on the URL of a JQuery AJAX call?

* just to clarify, my concern is more with the concatenation of the string. I wonder if there's a better way to construct the url without concatenating strings. Perhaps string formatting would be better?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I think Template literals is the neatest way to proceed here:

$.get(
    `users/${userId}/orders/${orderId}`,
    function (data) {
        //do something with the response
    }
);
about 4 years ago · Santiago Trujillo Relatório

0

You can make a function to make this request :

function makeOrderRequest(userId, orderId, sucessCallback) {
    $.get('users/' + userId + '/orders/' + orderId, successCallback);
}

And then call it when you need :

makeOrderRequest(1234, 8796, function(data){
     //do stuff
});
about 4 years ago · Santiago Trujillo 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