LoginSignup
17
14

More than 5 years have passed since last update.

GoogleAppsScriptでJSONPが "Refused to execute script"されたときの対処方法

Posted at

chromeのバージョンを31から36にアップデートしたら
別ドメインから
GoogleAppsScript.ContentServiceのJSONPが取得出来なくなった。

doGet.js
function doGet(e){
  var _c = e.parameter.callback;
  var _s = JSON.stringify({test:'hogehoge'});
  _s = ((_c)?_c + '(' + _s + ')':_s);
  return ContentService.createTextOutput(_s).setMimeType(ContentService.MimeType.JSON);
}

別サイトのHTML

index.html
<!DOCTYPE html>
<html ng-app="MyApp">

  <head>
    <script src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
    <div ng-controller="formCtrl">
      <form ng-submit="submit();">
        <button type="submit" />
      </form>
      <p>{{items|json}}</p>
    </div>
  </body>

</html>

script.js
angular.module("MyApp", [])
.controller("formCtrl", function($http, $scope){
  $scope.submit = function(){
    $http.jsonp('https://script.google.com/macros/s/xxxxxxxxxxxxxxxxxxxxxx/exec',{params:{callback:"JSON_CALLBACK",action:"hogehoge"}})
    .success(function(data, status){
      $scope.items = angular.fromJson(data);
    })
    .error(function(data, status, headers, config){
      console.log('error');
    });
  };
});

Angularjsのバージョンは1.3.0-beta.5

Chromeのコンソールログから出力されるエラーはこんな感じ。

Refused to execute script from 'https://script.google.com/macros/s/xxxxxxxxxxxxxxxxxxxxxx/exec?action=hogehoge&callback=angular.callbacks._0' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.

バージョンアップする度いつもなにかと悩んでるような気が・・・。う~む。

Chrome31ではJSONPでGASのJSONを取得出来ていたのでちと困った。

一日かかっていろいろと調べた結果、結局ブラウザのMIMETYPEがより厳密にチェックが入ったんだろうというオチに。
ContentService.createTextOutput(jsonp).setMimeType(ContentService.MimeType.JSON);

ContentService.createTextOutput(jsonp).setMimeType(ContentService.MimeType.JAVASCRIPT);

GAS側でMimeTypeをJAVASCRIPTにしたら解決。

参照

17
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
14