getJSON from Google Apps Script not working in Safari

1.1k views Asked by At

I've created this piece of code to handle my user login.

var gasUrl = "https://script.google.com/macros/s/[my id]/exec";
 function submitFormData() {
        var username = $("#inputEmail").val();
        var password = $("#inputPassword").val();
        $.getJSON(gasUrl + "?username=" + username + "&password=" + password, function(data) {
            if(data[0]) {
                    localStorage.setItem("username", data[0].username);
                    localStorage.setItem("id", data[0].id);
                    window.location.href = "/index.php";
                 }
                 else {
                    $(".reauwth-email").html("wrong");
                 }
            }); 
        }

Everything is working in Google Chrome but when I'm trying the same thing in Safari I get an error.

[Error] Failed to load resource: the server responded with a status of 405 (Method Not Allowed) (echo, line 0)
[Error] Failed to load resource: Cannot make any requests from null. (echo, line 0)
[Error] XMLHttpRequest cannot load [url]. Cannot make any requests from null. (login.php, line 0)

What am I doing wrong? The parameters are filled!

1

There are 1 answers

2
Spencer Easton On BEST ANSWER

This might be how Chrome and Safari handles XHR. Apps Script does not support CORS, so you would need to add the 'callback=?' parameter to $.getJson to indicate JSONP.

gasUrl + "?username=" + username + "&password=" + password +"&callback=?"

Then in your script you would wrap your return in the callback parameter.

function doGet(e){
  var callback = e.parameter.callback; // required for JSONP
  .
  .
  .
  return ContentService.createTextOutput(callback+'('+ JSON.stringify(returnObject)+')').setMimeType(ContentService.MimeType.JAVASCRIPT);
}