Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 515
Cesar Pantoja Avatar asked Oct 31 '25 12:10

Cesar Pantoja


1 Answers

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

$.get(
    `users/${userId}/orders/${orderId}`,
    function (data) {
        //do something with the response
    }
);
like image 156
Cesar Pantoja Avatar answered Nov 02 '25 01:11

Cesar Pantoja