Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

405 Method Not Allowed - Works on everything except iOS Safari

I've been trying to make an application in AngularJS that keeps track of some statistics and allows users to submit them to a google apps script for further processing. Everything works perfectly on the computer. I've tested it in chrome and in firefox, but when I try to submit on the iPad it shows the following errors:

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

Failed to load resource: Cannot make any requests from null.

XMLHttpRequest cannot load https://script.googleusercontent.com/macros/echo?user_content_key=UQXGbRq6...HLV301R. Cannot make any requests from null.

var URL = 'https://script.google.com/macros/s/.../exec';

$http.post(URL,
    $.param({ packet: JSON.stringify($scope.data) }), {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        }
    }
).success(function(data, status) {
    alert(data.packet);
    $scope.rollover();
}).error(function(data, status) {
    console.log(data);
    console.log(status);
    $scope.show_status(data);
});

It prints a couple lines in the console. The data line is blank and the returned status is 404.

Please help!

like image 564
Danny Avatar asked Nov 10 '22 09:11

Danny


1 Answers

Please see https://code.google.com/p/google-apps-script-issues/issues/detail?can=2&start=0&num=100&q=&colspec=Stars%20Opened%20ID%20Type%20Status%20Summary%20Component%20Owner&groupby=&sort=&id=3226

This issue is still yet to be resolved with no current date set to be fixed.

I believe the root cause is due to the google apps script not being able to handle to OPTIONS method that is used by Safari for 'preflighting' non-standard requests to other domains.

The work around I am using is to make a request to my server to then interact with the google apps script. Not beautiful and slow, but I need to ensure the site is working.

Here is the php code I used to make the request on my server, passing in the macro, id and sheet as get params. The php then returns the retrieved data from google:

<?php

header('Access-Control-Allow-Origin: *');
$url = 'https://script.google.com/macros/s/';
$url .= $_GET['macro'];
$url .= '/exec?id=';
$url .= $_GET['id'];
$url .= '&sheet=';
$url .= $_GET['sheet'];

function get_data2($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}


$response = get_data2($url);

echo $response;

?>

This now means that Safari is making a request to the same domain and also we are following the 403 redirect note google kindly puts in our way with CURLOPT_FOLLOWLOCATION.

update 7th May 2015

I've noticed that although I'm receiving a 405 method not allowed response from the server, data is actually being successfully received by the Google App Script. For example I've been able to receive the get and post parameters with no issues.

To accommodate for this I've added in a switch in the error catch from the AJAX call as the error code I'm actually receiving is 0... happy to see that something is working but I wouldn't trust it too much in production.

like image 65
David Avatar answered Nov 14 '22 21:11

David