Reputation: 386
I have the below snippet which makes a call to my API and returns some JSON
var authorizationToken = "xxxxxxxxxxxxxxx";
function myapiRequest(endpoint, options) {
$.ajax($.extend({}, {
type: 'GET',
dataType: "json",
success: function(data) {
$('.Name').html(data.user.name);
$('.Email').html(data.user.email);
$('.Address').html(data.user.teams[0].name);
},
url: "https://api.myapi.com/" + endpoint,
headers: {
"Authorization": "Token token=" + authorizationToken,
"Accept": "application/vnd.myapi+json;version=2"
}
},
options));
}
myapiRequest('/users/' + 'usersID' + '?include%5B%5D=contact_methods&include%5B%5D=teams'); //This is where my userID variable will be called
I would like to add something further like....
var currentLocation = window.location; // https://myapi.com/users/PLLFFR6
var usersID = PLLFFR6
To get the current windows URL and strip the user ID 'PLLFFR6' out and use that as a variable later on my URL always follows the same pattern, /users/ID is the ID part I want to use only
myapiRequest('/users/' + 'usersID' + '?include%5B%5D=contact_methods&include%5B%5D=teams');
Upvotes: 2
Views: 119
Reputation: 164732
Do you just mean something like this...
var usersID = location.pathname.split('/').pop()
I'd also make use of the $.ajax
data
property for those query parameters, ie
myapiRequest(`/users/${usersID}`, { // or "'/users/' + usersID" if you don't like template literals
data: {
include: ['contact_methods', 'teams']
}
})
Upvotes: 0
Reputation: 4590
If your URL is in the format https://myapi.com/users/PLLFFR6
then you can split
the url based on /
and take the last array element. Example below:
<script>
window.onload = function () { test(); }
function test() {
var url = "https://myapi.com/users/PLLFFR6".split("/");
url = url[url.length-1]
alert(url);
}
</script>
Upvotes: 1