Help with API call

Raymond_DidonatoRaymond_Didonato Customer IT Monkey ✭
Hello community,

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?

  getJSON();
  function getJSON() {
     let xhr = new XMLHttpRequest();
     xhr.open('GET', '/api/V3/WorkItem/GetGridWorkItemsAll',
    { 
    "userId": session.user.Id,
    "isScoped": false,
    "showActivities": false,
    "showInactiveItems": false 
    }, true);
   xhr.onload = function() {
   let myJSON = JSON.parse(xhr.responseText);
   console.log(myJSON);
   }
 }

Thanks for the help!

Best Answer

  • Raymond_DidonatoRaymond_Didonato Customer IT Monkey ✭
    Accepted Answer
    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!

Answers

  • Roland_KindRoland_Kind Partner Advanced IT Monkey ✭✭✭
    edited June 2018

    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

  • Raymond_DidonatoRaymond_Didonato Customer IT Monkey ✭
    I thought at the end of last week that I could type session.user.id in the browser console and it was returning my user guid, but today it is not.  So certainly, that is something that will cause an issue.

    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.


  • Raymond_DidonatoRaymond_Didonato Customer IT Monkey ✭
    My mistake, it does return my guid with session.user.Id

    Gotta watch those cases with JS.
  • Roland_KindRoland_Kind Partner Advanced IT Monkey ✭✭✭

    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 ...

  • Raymond_DidonatoRaymond_Didonato Customer IT Monkey ✭
    Accepted Answer
    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!
  • lance_wynnlance_wynn Cireson Dev IT Monkey ✭
    Hi,
    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.
  • Raymond_DidonatoRaymond_Didonato Customer IT Monkey ✭
    Thanks for the follow up Lance.  I'm no longer actively working on this solution, but we may revisit it down the road.
Sign In or Register to comment.