1

This issue is very similar to others (like Google Drive Page Not Found - Sorry, unable to open the file at this time for example) posted here. It's not exactly the same, but I do believe it has the same root issue illustrated in that post in that trying to submit a form to a Google App Script while logged into more than to 1 Google account causes /u/1 and/or /u/0 to be added to the script's URL thus producing a 404 Error.

This is using a standard Google account - not G-Suite.

I have a form on a website that submits to a Google Apps Script via AJAX. The script makes some API calls to create a Google Doc containing the data collected by the form.

HTML/Javascript:

<form>
  <input type="text" name="full_name">
  <input type="text" name="phone">
  <input type="submit">
</form>

$('form').submit(function() {
  var obj = $(this).serializeObject();
  var gurl = "https://script.google.com/macros/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec";

  $.ajax({
    url: gurl,
    type: "GET",
    data: obj,
    dataType: "jsonp",
    success: function(data, status, xhr) {
      console.log("success");
      console.log(data);
    });
});

GoogleScripts

function doGet(e) {
  var params = e.parameters
  var result = {};
  try {
    result = {
      status: start(params),
      msg: 'Success',
      vals: formData,
      rawVals: params,
      errs: errors
    }
  } catch (f) {
    result.error = f.toString();
 }
 return ContentService     
  .createTextOutput(e.parameters.callback + '(' + JSON.stringify(result) + ')')
  .setMimeType(ContentService.MimeType.JAVASCRIPT);
}

Submitting the form while logged into more than 1 Google account in the same browser results in the following error in the console and the form does nothing:

jquery.js?ver=1.12.4-wp:4 GET https://script.google.com/macros/u/1/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec?callback=jQuery112407830193282901534_1608623376571&s&full_name=Dave+Pe&phone=1111111111_=1608623376572 net::ERR_ABORTED 404

When I go to Network tab to view the request, the Header tab there shows the following:

Request URL: https://script.google.com/macros/u/1/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec?callback=jQuery112407830193282901534_1608623376571&s&full_name=Dave+Pe&phone=1111111111_=1608623376572

Notice the /u/1/ that have been inserted into the URL that are not present in the URL I pass to my $.ajax() call.

Most of the answers I've found for this issue say to just remove the /u/1/, but since I didn't add it in the 1st place, I don't know where I would remove it from.

Can anyone confirm that this seemingly known issue (of having the URL altered when logged into multiple Google accounts) is what is causing my problems? Any ideas as to how I can go about making my request to:

https://script.google.com/macros/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec

and not

https://script.google.com/macros/u/1/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec

?? or is there something more deeply wrong with the way I'm trying to use Google Scripts here?

Daveh0
  • 952
  • 9
  • 33
  • I cannot understand about `I do believe the issue is with trying to access a GoogleScript while logged into more than 1 account and having /u/1 and /u/0 prepended to the script's URL.`. I apologize for this. Can I ask you about the detail of your current issue and your goal? – Tanaike Dec 22 '20 at 23:01
  • @Tanaike - sorry, that was referring to the problem identified in the other SO post I referenced (https://stackoverflow.com/questions/47045209/google-drive-page-not-found-sorry-unable-to-open-the-file-at-this-time). In that post, the cause of the issue is that the user is logged into more than 1 Google accounts at the same time. I just meant that I believe that issue is causing the problem here too. My goal is to submit form data collected on my website to a Google App Script. My issue is that the request URL is somehow being modified to include `u/1` which results in a 404 error – Daveh0 Dec 23 '20 at 03:24

3 Answers3

1

My solution was to move the request from the client side to the server. I submit my form values to a server-side page via AJAX and then on that page, I make an HTTP request with cURL to my Google Apps Script (sending the form data in the body) and send the response back to the client.

Seems to be working... no issues I can think of but that doesn't mean they don't exist. If there are any holes to be shot in that approach, please feel free to unload.

Watered down...

JavaScript

$.ajax({
    url: '/ajax-handler.php',
    type: "POST",
    data: obj
    success: function(data, status, xhr) {
      console.log("success");
      console.log(data);
    });
});

PHP

$vals = my_sanitize_func($_POST);

$url = GAS_URL;
$fields = $vals;

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

// handle/format response;
$result = ...

print $result;
Daveh0
  • 952
  • 9
  • 33
1

You can also do whatever you want this way.
Google App Script (Code.gs):

function doGet(e){
     var action = e.parameter.action;
     if (action=="sendSuccess"){
        justSendSuccess(e);
      }
   }

   function justSendSuccess(e){
   var output = JSON.stringify({"result":"success"});
        return ContentService
          .createTextOutput(e.parameter.callback+"("+ output + ");")
          .setMimeType(ContentService.MimeType.JAVASCRIPT);
}

JavaScript Part:

function callGoogleScript(){
        console.log("Started");
        var url = "https://script.google.com/macros/s/###SCRIPT_ID###/exec";
        $.ajax({
        crossDomain: true,
        url: url,
        data: {
        "action":"setInfo"
        },
        method: "GET",
        dataType: 'jsonp',
        jsonp: "callback",
        success: function(data){
        console.log("SUCCESS!");
        console.log(data);
  }
});

    }
  • This is essentially the code in OP, which seems to be working now. perhaps the bug (https://issuetracker.google.com/72798634) has been resolved... – Daveh0 Apr 23 '21 at 21:04
0

I tried to run it (authenticated with a single account) and it seems to work. This error seems to happen because you are authenticated with multiple accounts. Also, it seems as it has already been documented at Google Issue Tracker (link to issue). If you want it to make it more visible, you can click the white star (☆) which tells google that you are affected by this issue.

As a side note, notice that the code you make will be executed by everyone as you. This code will have your privileges. Be very careful. Your account limits may also apply.

Martí
  • 2,651
  • 1
  • 4
  • 11
  • so are you saying that there is no solution to my problem due to this known issue with Google? Any suggestions for workarounds? – Daveh0 Jan 09 '21 at 11:25
  • This bug only happens when logging with multiple accounts at the same time. So log with a single account at a time. You can use incognito or a session container extension (so there's no need to log in every time). – Martí Jan 11 '21 at 08:15
  • 1
    it's not me, it's the users filling out the form... i have no control over how many of their accounts they're logged into – Daveh0 Jan 11 '21 at 13:42