Request to Google Apps Script URL for deployed WebApp produces 404 error

Tags: , , ,



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?

Answer

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;


Source: stackoverflow