2 Replies Latest reply on Jun 2, 2017 12:36 PM by Jeff Shurtliff

    Retrieve the Place Name using JavaScript and Ajax

    Jeff Shurtliff

      Hey all,

       

      Can someone please point me in the right direction on how I can retrieve the name of the current Place using JavaScript and Ajax?

      I've been searching all over JiveWorks, Stack Overflow, and everywhere else I can and can't seem to find what I'm looking for.

       

      Basically I'm trying to perform a GET request to pull the JSON for a Place ID, parse it, and return the name.

       

      This is the code that I have so far (which resides within an HTML widget), but it is returning errors.

       

      <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
      
      
      <p id="placeInfo"></p>
      
      
      <script>
      var placeID = window.parent.jive.global.containerBrowseId;
      var placeURL = '/api/core/v3/places/' + placeID;
      console.log('placeURL: ' + placeURL)
      
      
      var $j = jQuery.noConflict();
          $j.ajax({
              type: 'GET',
              url: placeURL,
              dataType: 'json',
        contentType: 'application/json',
              cache: false,
              success: function (data) {
                  var cleanedJSON = JSON.parse(data.replace("throw 'allowIllegalResourceCall is false.';", ""));
                  jivePlaceData = []; // Global array
                  $j(cleanedJSON.list).each(function () {
                      jivePlaceData.push(this.name);
                  });
                  console.log(jivePlaceData);
                  document.getElementById("placeInfo").innerHTML = 'Place Data: <br \>' + jivePlaceData;
              },
              error: function (data) {
                  console.log('Error getting jivePlaceData!');
              }
          });
      
      
      setTimeout(resizeMe,2000);
      </script>
      

       

      This is what I'm seeing in the console when the page loads:

       

      Any thoughts on what I'm doing wrong and/or what I need to do differently?

       

      Thanks!
      Jeff

        • Re: Retrieve the Place Name using JavaScript and Ajax
          Ryan Rutan

          Depends on where you are trying to put this code.  If it's in a Tile, you should be able to just use our shim, see: jive-derby/main.js at master · jivesoftware/jive-derby · GitHub

          if an App, this shim: jive-derby/main.js at master · jivesoftware/jive-derby · GitHub

           

          If inside the Theme, then you could do the following:

           

          j.ajax call to this URL:

          "{host}/api/core/v3/places?filter=entityDescriptor("+containerID+","+containerType+")"

          Note: containerID and containerType are runtime variables in JS that are available in the UI when relevant.  It's not an API per say but they've been there since Jive 1.0

          Another way, if you are only a landing page the URL is clean (i.e. /groups/xxxx or /community/xxxxx, etc..) you can make a request to /groups/xxxxxx/api/v3 and get the same payload as above.

           

          Hope one of those helps.

          2 people found this helpful
            • Re: Retrieve the Place Name using JavaScript and Ajax
              Jeff Shurtliff

              Hi Ryan,

               

              Thanks for the help with this.  We were able to resolve the issue with the code below.

              var placeID = window.jive.global.containerBrowseId;
              var placeURL = '/api/core/v3/places/' + placeID;
              
              
              var $j = jQuery.noConflict();
                $j.ajax({
                type: 'GET',
                url: placeURL,
                dataType: 'json',
                contentType: 'application/json',
                cache: false,
                success: function (data) {
                var cleanedJSON = JSON.stringify(data).replace("throw 'allowIllegalResourceCall is false.';", "");  // legacy error handling
                cleanedJSON = JSON.parse(cleanedJSON);
                jivePlaceData = []; // Global array
                $j(cleanedJSON).each(function () {
                jivePlaceData.push(this.name);
                });
                communityName = String(jivePlaceData);
                console.log('Community Name: ' + communityName);
                },
                error: function (data) {
                console.log('Error getting jivePlaceData!');
                }
                });
              

               

              Thanks,
              Jeff

              1 person found this helpful