title: Generate tableau trusted ticket using AJAX
body: <p>We are trying to get a trusted ticket from Tableau Server using Ajax.
  Tableau team provides support for php, java, sharepoint, ruby but not for Ajax.</p>
  
  <p>The code we are working is:</p>
  
  <pre><code>&lt;script type="text/javascript" src="../js/jquery-1.7.1.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="../js/tableau_v8.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="jsonp.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  
  $(document).ready(function () {
      $("#tableau").each(function () {
  
          var workbook = $(this).attr("Presents");
          var view = $(this).attr("MyDashboard");
          var username = $(this).attr("userx");
          var ticket;
  
          $.ajax({
              type: 'POST',
              url: 'http://serverurl/trusted',
              contentType: 'application/json',
              dataType: 'jsonp',
                  data: { 
              username:'registeredusername',
              server:'url},
              success: function (result) {
                  alert('Ok');
                  ticket = result;
              },
              error: function () { 
                  //console.log('Erro');
                  alert('Error'); 
              }
          });
          var url = "http://serverurl/trusted/" + ticket + "/views/" + workbook + "/" + view;
          var options = {
              width: this.offsetWidth,
              height: this.offsetHeight,
              hideTabs: true,
              hideToolbar: false,
              onFirstInteractive: function () {
              workbook = viz.getWorkbook();
              activeSheet = workbook.getActiveSheet();
              }
  
              };
          viz = new tableauSoftware.Viz(this, url, options);
      });
  
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>To give you an idea, please find below how it works in php:</p>
  
  <p>TABLEAU_TRUSTED.php</p>
  
  <pre><code>&lt;?php
  
  // Returns a trusted URL for a view on a server for the
  // given user.  For example, if the URL of the view is:
  //    http://tabserver/views/MyWorkbook/MyView
  //
  // Then:
  //   $server = "tabserver";
  //   $view_url = "views/MyWorkbook/MyView";
  //
  function get_trusted_url($user,$server,$view_url) {
    $params = ':embed=yes&amp;:toolbar=yes';
  
  
    $ticket = get_trusted_ticket($server, $user, $_SERVER['REMOTE_ADDR']);
    if($ticket &gt; 0) {
      return "http://$server/trusted/$ticket/$view_url?$params";
    }
    else 
      return 0;
  }
  
  // Note that this function requires the pecl_http extension. 
  // See: http://pecl.php.net/package/pecl_http
  
  // the client_ip parameter isn't necessary to send in the POST unless you have
  // wgserver.extended_trusted_ip_checking enabled (it's disabled by default)
  Function get_trusted_ticket($wgserver, $user, $remote_addr) {
    $params = array(
      'username' =&gt; $user,
      'client_ip' =&gt; $remote_addr
    );
  
    return http_parse_message(http_post_fields("http://$wgserver/trusted", $params))-&gt;body;
  }
  
  ?&gt;
  </code></pre>
  
  <p>TABLEAU_SAMPLE_VIEW_PAGE.php</p>
  
  <pre><code>    &lt;p&gt;An embedded view appears below:&lt;/p&gt;
  
  &lt;?php
  
  // This user-provided library should define get_user(), which returns the 
  // name of the user currently logged into this application.
  //
  include 'auth.php';
  
  // Tableau-provided functions for doing trusted authentication
  include 'tableau_trusted.php';
  
  ?&gt;
  
  &lt;iframe src="&lt;?php echo get_trusted_url(get_user(),'localhost','views/Date-Time/DateCalcs')?&gt;"
          width="400" height="400"&gt;
  &lt;/iframe&gt;
  
  &lt;p&gt;
  This was created using trusted authentication.
  &lt;/p&gt;
  </code></pre>
  
  <p>Any ideas how this php could work in javascript and ajax?
  Any ideas how could we improve our code?</p>
  
  <p>Thanks, Gabriel</p>