5 Replies Latest reply on Aug 8, 2014 1:10 PM by dbgilbert

    Calling Jive rest v3 api using ajax

    ragushiva

      Hi I am trying to access the jive rest api by writing a java script as below.

       

      $( "#buttionId" ).click(function() {

        alert( "Handler for .click() called." );

        $.ajax( {

      url : 'http://mydomain.com/api/core/v3/activities',

      dataType : 'jsonnp',

      type: 'GET',

      headers : {Accept : "application/json","Access-Control-Allow-Origin" : "*"},

      crossDomain : true,

      beforeSend : function(xhr) {

               

                xhr.setRequestHeader("Authorization", "Basic cy5zaGFua2FyLnJhZ2h1cmFtYW46MTIzNDU2");

      },

      error : function(xhr, ajaxOptions, thrownError) {

        alert("Error not able to access api");

      },

      success : function(model) {

         alert("success");

      }

      });


      It is Still giving the Origin null is not allowed by Access-Control-Allow-Origin. 

      please help.

        • Re: Calling Jive rest v3 api using ajax

          Is your code deployed on the Jive server or somewhere else? If hosted somewhere else you are running into the standard cross domain issues with JS:

           

          The Jive Apis do not support JSONP in the first place, afaik. Depending on what you are trying to achieve, you might need to go with a server side solutions, or host your code on the jive server

          • Re: Calling Jive rest v3 api using ajax
            dbgilbert

            The same-origin policy is a pretty common stumbling block for AJAX. Basically you can't communicate outside the domain of the page making the AJAX request, so www.yourwebsite.com could make requests to www.yourwebsite.com/yourService but not to www.mywebsite.com/myAPI.

             

            JSONP is a common workaround, but Jive does not support it. The solution I'd recommend is a proxy-webservice to go with your page making AJAX requests. By having the service share a domain (or support cross-domain via jsonp or CORS), you can access it with AJAX. So instead of making your direct call, you do something like the below:


            $.ajax( {

            url : 'http://myProxyService.com',

            dataType : 'json',

            type: 'POST',

            data: "{

                 URL: 'http://mydomain.com/api/core/v3/activities',

                 headers : <yourHeaders>,

                 args: <yourArgs>

            }",

            ...

             

            And then the proxy service reads the parameters passed to it in the AJAX request and uses them to form the request you wanted to make to Jive, gets the response, and passes it back to you. It adds a bit of overhead, but unless you can run your code on Jive's server making the request directly via browser is simply not possible.