Help with API call
Ever since implementing the Cireson Portal, and seeing some of the customizations, I've been drawn to learning more about web development. My manager asked me to see if there was a way to have a calendar display CRs according to their End Date, for better visibility. I wasn't sure if there was something built in for this, and honestly, I found it to be a good opportunity to take a crack at developing my own solution.
So far, I have a page that displays a calendar and references a JS file. I have static JSON data in the JS file, which I loop through to find CR data and add it as links to the calendar page. While that is all working fine, it's static. I've been around the forums and various JS tutorials looking for how to make the API call correctly. Also, keep in mind that I've only learned some vanilla JS so far, no jQuery.
Whenever I run the code that I think could work, I receive an error that "session" is not defined. Here's a snippet of what the code I'm trying to run. I assume that it is wrong, let me get that out of the way, but it feels like I'm close. Anyone here that can help fill in my knowledge gap?
Thanks for the help!
Best Answer
-
Raymond_Didonato Customer IT Monkey ✭So, after trial and many hours of error, I found something that acutally worked:$(document).ready(function() {
let userId = session.user.Id;let xhr = new XMLHttpRequest();xhr.open('GET','/api/V3/WorkItem/GetGridWorkItemsAll?userId=' + userId + '&isScoped=false&showActivities=false&showInactiveItems=false', true);xhr.responseType = 'text';xhr.send();
xhr.onload = function() {if(xhr.status === 200) {let myJSON = JSON.parse(xhr.responseText);console.log(myJSON);}}});
Now to figure out how to work my existing code around this, but if anyone has any other tips or suggestions, they are welcome!0
Answers
Have you tried window.session.user.Id ?
btw: when using $.ajax calls i use the following syntax:
$.ajax({
url: "/api/V3/Projection/CreateProjectionByTemplate",
data: {id: template, createdById: uid},
type: "GET",
context: dataItem,
....
e.g. URL,data without quotes
However, I can run something like this directly in the browser console, which is borrowed from another community solution, and I assume is in jQuery. It returns a response, which is how I originally obtained the static data that I have in my JS file.
$.getJSON('/api/V3/WorkItem/GetMyTeamRequest',{"userId":session.user.Id,"showInactiveItems":false,"isScoped":false});
The custom solution that is from also apparently triggers on document ready, or at least that's what I get out of $(document).ready(function() ....
I would really like it if we could keep things strictly vanilla JS, but at this point whatever takes me to get it to work, and then I can understand it. This is not a production issue, I'm playing around in test.
Gotta watch those cases with JS.
Hi - I am not sure - is this solved now ?
if not - just one additional thought - without testing it: I could be a timing problem - so depending on the execution point of your script - session.user.id could be undefined because the according js is not executed at this time which defines session.user.id ...
let userId = session.user.Id;
xhr.onload = function() {
Now to figure out how to work my existing code around this, but if anyone has any other tips or suggestions, they are welcome!
This is a bit old, but if you have not yet found a solution, perhaps you could post the existing js that is responsible for parsing the static json that you reference.