title: Ajax query failing due to OIDC SSO redirect
body: <p>I'm wondering what the standard solution is to ajax failing due to (OIDC) SSO redirect/refresh. It's insidious because the failure is silent, the web page <em>appears</em> to be working properly.</p>
  <p>I have a simple web page with static HTML and simple javascript that populates the page via ajax and refreshes it periodically.</p>
  <p>The webpage and JS are both access-controlled via the same OIDC SSO. This works, but can fail in the following ways when the ajax call is rejected 401 due to needing an authentication refresh. (This is not full password authentication, this is just &quot;check that my token is ok, and see that it is, and keep going as if nothing had happened&quot;.)</p>
  <p>Back end and front end are both served from the same server by a basic Apache service with the same Access Control and Authorization requirements.</p>
  <ol>
  <li>If a user navigates to the page in such a way that a cached version of the HTML is loaded and just the ajax runs. (e.g. back button)</li>
  <li>If the page is left sitting for long enough, I believe it refreshes will also fail for the same reason.</li>
  </ol>
  <p>I have worked around the issue as shown below, but it feels like a hack, like there must be some much more standard way to do this.</p>
  <pre class="lang-js prettyprint-override"><code>// This function is called every 30s on a timer
  function updateData(args, callback) {
      $.ajax({
          xhrFields: { withCredentials: true },
          url: &quot;/getit?&quot; + args,
          success: function(data) {
              localStorage.removeItem('pagereloaded')
              callback(data);
          },
          statusCode: {
              // Reload is because SSO tokens can timeout causing ajax to fail.
              // In these cases we want to reload the page right away. 
              // But what if it's just a genuine auth failure, we do not want to go into an infinite reload loop.
              // So pagereloaded and timer try to reload quickly a first time, but then avoid a loop after that.
              401: function () {               
                  if (localStorage.getItem('pagereloaded') == null || (Date.now() - start_time) &gt; 60000) {
                      localStorage.setItem(&quot;pagereloaded&quot;, 1)
                      location.reload();
                  }
              }
          }
      });
  }
  </code></pre>