title: How can I do Cross-Origin in the google app script?
body: <p>I tried several tutorials and nothing worked.</p>
  
  <p>My problem is as follows, if I click on an html link it opens the other page, but if I try to open via form it always shows a cross-origin error.</p>
  
  <p>I provide a public link with a copy of the main pages of my project. Could someone show me in practice how to solve this problem?</p>
  
  <p>Project:</p>
  
  <p>doGet.gs</p>
  
  <pre><code>function doGet(e) {
    if(!e.parameter.page){
      //When no specific page requested and no session or the page is not to register, return Login page;
      return(HtmlService.createTemplateFromFile('page').evaluate()
             .setTitle('Page 1')
             .setSandboxMode(HtmlService.SandboxMode.IFRAME)
            );
    } else {
      //Use page parameter to pick an html file from the script;
      return(HtmlService.createTemplateFromFile(e.parameter['page']).evaluate()
             .setTitle(e.parameter['title'])
             .setSandboxMode(HtmlService.SandboxMode.IFRAME)
            );
    }
  }
  
  /*==================== That code goes include files in the page. ====================*/
  function include(filename) {
    return(HtmlService.createHtmlOutputFromFile(filename)
           .setSandboxMode(HtmlService.SandboxMode.IFRAME)
           .getContent()
          );
  }
  
  /*==================== Get the URL for the Google Apps Script running as a WebApp. ====================*/
  function getScriptUrl(){
    var url = ScriptApp.getService().getUrl();
    return(url);
  }
  </code></pre>
  
  <p>javaScript.html</p>
  
  <pre><code>&lt;script&gt;
  /*==================== Attach a submit handler to the form. ====================*/
  $("[name='loginForm']").submit(function(event){
    var $form;
    var login;
    var url;
  
    //Stop form from submitting normally.
    event.preventDefault();
  
    //Get some values from elements on the page:
    $form = $(this);
    login = {};
  
    login.email = $form.find("#inputUser").val();
    login.senha = $form.find("#inputPassword").val();
    url = $form.attr("action");
  
    var posting = $.post(url, login);
  
    //Post sucess.
    posting.done(function(data){
      console.log(data);
  
      //Redirect to page2.
      //var geting = $.get(url, login);
    });
  });
  
  /*==================== Get app url. ====================*/
  $(function() {
    google.script.run.withSuccessHandler(setHref)
      .getScriptUrl();
  });
  
  /*==================== Modifying link of navbar. ====================*/
  function setHref(url){
    var links = $("a[href], form[action]");
  
    for(var i = 0; i &lt; links.length; i++){
      var page;
  
      if(links[i].tagName == "A"){
        page = links[i].getAttributeNode("href").value;
        links[i].getAttributeNode("href").value = url + page;
      } else {
        page = links[i].getAttributeNode("action").value;
        links[i].getAttributeNode("action").value = url + page;
      }
    }
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>Page</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="pt-br"&gt;
    &lt;head&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;form name="loginForm" action="?page=page2&amp;title='Page 2'" method="POST"&gt;
        &lt;input type="text" id="inputUser" name="inputUser" value=""/&gt;
        &lt;svg height="10" width="10"&gt;
        &lt;path d="M4 0c-1.105 0-2 1.119-2 2.5s.895 2.5 2 2.5 2-1.119 2-2.5-.895-2.5-2-2.5zm-2.094 5c-1.07.04-1.906.92-1.906
        2v1h8v-1c0-1.08-.836-1.96-1.906-2-.54.61-1.284 1-2.094 1-.81 0-1.554-.39-2.094-1z" id="person"&gt;&lt;/path&gt;
        &lt;/svg&gt;
        Usuário
        &lt;input type="password" id="inputPassword" name="inputPassword" value=""/&gt;
        &lt;svg height="10" width="10"&gt;
        &lt;path d="M5.5 0c-1.38 0-2.5 1.12-2.5 2.5 0 .16.033.297.063.438l-3.063 3.063v2h3v-2h2v-1l.063-.063c.14.03.277.063.438.063
        1.38 0 2.5-1.12 2.5-2.5s-1.12-2.5-2.5-2.5zm.5 1c.55 0 1 .45 1 1s-.45 1-1 1-1-.45-1-1 .45-1 1-1z" id="key"&gt;&lt;/path&gt;
        &lt;/svg&gt;
        Senha
        &lt;button type="submit"&gt;Login&lt;/button&gt;
      &lt;/form&gt;
  
      &lt;!-- ================================================== --&gt;
      &lt;!-- jQuery --&gt;
      &lt;?!= include('jQuery'); ?&gt;
      &lt;!-- Standard JavaScript --&gt;
      &lt;?!= include('javaScript'); ?&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>doPost</p>
  
  <pre><code>function doPost(e){
    var callback;
    var output;
    var data;
  
    data.e = e;
    data.url = getScriptUrl();
  
    //Returning the response to the application.
    callback = e.parameter.callback;
    output  = ContentService.createTextOutput();
  
    if (callback === undefined) {
      output.setContent(JSON.stringify(data));
    } else {
      output.setContent(callback + "(" + JSON.stringify(data) + ")");
    }
  
    output.setMimeType(ContentService.MimeType.JSON);
  
    return(output);
  }
  </code></pre>
  
  <p>page2</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="pt-br"&gt;
    &lt;head&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div id="result"&gt;
      &lt;/div&gt;
  
      &lt;!-- ================================================== --&gt;
      &lt;!-- jQuery --&gt;
      &lt;?!= include('jQuery'); ?&gt;
      &lt;!-- Standard JavaScript --&gt;
      &lt;?!= include('javaScript'); ?&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is the error, is caused when I click to submit form, in the submit function in javascript.html</p>
  
  <blockquote>
    <p>Failed to load
    <a href="https://script.google.com/macros/s/AKfycbyzqu-axemMxSmafZdEpqu1uYssWMM9SAleKzmPB_Tu-0rF8vnk/exec?page=page2&amp;title=%27Page%202%27" rel="nofollow noreferrer">https://script.google.com/macros/s/AKfycbyzqu-axemMxSmafZdEpqu1uYssWMM9SAleKzmPB_Tu-0rF8vnk/exec?page=page2&amp;title=%27Page%202%27</a>:
    No 'Access-Control-Allow-Origin' header is present on the requested
    resource. Origin
    '<a href="https://n-iqrbuxm45ga265etahrrml6j6klncaly5mdwd3y-0lu-script.googleusercontent.com" rel="nofollow noreferrer">https://n-iqrbuxm45ga265etahrrml6j6klncaly5mdwd3y-0lu-script.googleusercontent.com</a>'
    is therefore not allowed access.</p>
  </blockquote>