Creating web service with GAS using JSONP

I have the following code.

var json = callback + '(' + Utilities.jsonStringify(range.getValues()) +')';

return ContentService.createTextOutput(json).setMimeType(ContentService.MimeType.JAVASCRIPT);

When I call that app from another page I get the following error:

Refused to execute script from 'https://script.google.com/macros/s//exec?jsonp=callback' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled. How do I get around this issue. It LOOKS like the generating code sets the mime type but it is not being honored. On the other end, I generate a element and also set the mimetype to text/javascript but that doesn't fix it.


Just managed to make it, on doGet i return:

  return ContentService.createTextOutput(
    'on_result(' + JSON.stringify({hello:1}) + ')')
    .setMimeType(ContentService.MimeType.JAVASCRIPT);

and on jQuery side i do

$.ajax({
  url: "YOUR_SCRIPT_URL",
  type: "GET",
  dataType: "jsonp",
  crossDomain: true,
  jsonpCallback: "on_result",
  success: function() {
    return console.log(arguments);
  },
  error: function() {
    return console.error(arguments);
  }
});

and then i get a "success" result. it hurts, but it works.


Whatever's returning your JSONP is returning it with the wrong MIME type. The correct MIME type for JSONP is application/javascript , because it's really just JavaScript code.

Here's an illustration of the difference between JSON and JSONP:

Here's some JSON:

{"foo": "bar"}

...which would appropriately be served with the MIME type application/json .

Here's the same thing expressed as JSONP (which is to say, JavaScript):

someCallbackName({"foo": "bar"})

...which would appropriately be served as application/javascript .

链接地址: http://www.djcxy.com/p/47598.html

上一篇: 由于错误的MIME类型,Chrome拒绝执行AJAX脚本

下一篇: 使用JSONP使用GAS创建Web服务