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?
I think Template literals is the neatest way to proceed here:
$.get(
`users/${userId}/orders/${orderId}`,
function (data) {
//do something with the response
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With