如何解决 'MIME type mismatch error' 从我的 Google Apps 脚本中阻止 AJAX 请求的资源?
How do I resolve a 'MIME type mismatch error' blocking an AJAX-requested resource from my Google Apps Script?
我正在尝试使用 Jquery.ajax
而不是 fetch
来实现代码 。
我在调用 AJAX 时收到以下错误:
The resource from “https://script.googleusercontent.com/macros/echo?...” was blocked due to MIME type (“application/json”) mismatch (X-Content-Type-Options: nosniff).
Google Apps 脚本可以与 cURL 一起正常工作,所以我怀疑我的(非常简单的)GApps 脚本和我的客户端脚本之间存在一些分歧。
这是 GApps 脚本:
function doGet(e) {
const id = e.parameter.spreadsheetId;
const sheetName = e.parameter.sheetName;
const sheet = SpreadsheetApp.openById(id).getSheetByName(sheetName);
const values = sheet.getDataRange().getValues();
return ContentService.createTextOutput(JSON.stringify({values: values})).setMimeType(ContentService.MimeType.JAVASCRIPT);
}
注意:我尝试将最后一行的 MimeType
更改为 JSON
但没有成功。
这是我在客户端脚本中调用的 AJAX 设置:
var settings = {
'cache': false,
'dataType': "jsonp",
'async': true,
'crossDomain': true,
'url': url,
'method': 'GET',
'headers': {
'accept': 'application/json',
'Access-Control-Allow-Origin': '*'
}
};
我在 Firefox 中工作。在 Chrome 中抛出类似的错误。
我该如何解决?
我相信你的目标如下。
- 您想使用
jQuery.ajax()
而不是 fetch
向 Web 应用程序发出请求。
为此,当你的settings
修改后,下面的修改如何?
修改后的脚本:
var settings = {
'url': url,
'method': 'GET',
'dataType':"json",
'data': {spreadsheetId: "###SpreadsheetId###", sheetName: "###SheetName###"},
};
示例脚本:
var url = 'https://script.google.com/macros/s/###/exec';
var settings = {
'url': url,
'method': 'GET',
'dataType':"json",
'data': {spreadsheetId: "###SpreadsheetId###", sheetName: "###SheetName###"},
};
$.ajax(settings).done(res => {
console.log(res.values); // By this, you can retrieve the values of `{values: values}` from Web Apps.
}).fail(err => console.log(err));
注:
- 当您修改了 Web 应用程序的 Google Apps 脚本时,请将 Web 应用程序重新部署为新版本。由此,最新的脚本被反映到Web Apps。请注意这一点。
- 在上面修改后的脚本中,可以使用您的 Google Apps 脚本。
- 在这种情况下,我可以确认上述脚本适用于 Chrome。
参考:
我正在尝试使用 Jquery.ajax
而不是 fetch
来实现代码
我在调用 AJAX 时收到以下错误:
The resource from “https://script.googleusercontent.com/macros/echo?...” was blocked due to MIME type (“application/json”) mismatch (X-Content-Type-Options: nosniff).
Google Apps 脚本可以与 cURL 一起正常工作,所以我怀疑我的(非常简单的)GApps 脚本和我的客户端脚本之间存在一些分歧。
这是 GApps 脚本:
function doGet(e) {
const id = e.parameter.spreadsheetId;
const sheetName = e.parameter.sheetName;
const sheet = SpreadsheetApp.openById(id).getSheetByName(sheetName);
const values = sheet.getDataRange().getValues();
return ContentService.createTextOutput(JSON.stringify({values: values})).setMimeType(ContentService.MimeType.JAVASCRIPT);
}
注意:我尝试将最后一行的 MimeType
更改为 JSON
但没有成功。
这是我在客户端脚本中调用的 AJAX 设置:
var settings = {
'cache': false,
'dataType': "jsonp",
'async': true,
'crossDomain': true,
'url': url,
'method': 'GET',
'headers': {
'accept': 'application/json',
'Access-Control-Allow-Origin': '*'
}
};
我在 Firefox 中工作。在 Chrome 中抛出类似的错误。 我该如何解决?
我相信你的目标如下。
- 您想使用
jQuery.ajax()
而不是fetch
向 Web 应用程序发出请求。
为此,当你的settings
修改后,下面的修改如何?
修改后的脚本:
var settings = {
'url': url,
'method': 'GET',
'dataType':"json",
'data': {spreadsheetId: "###SpreadsheetId###", sheetName: "###SheetName###"},
};
示例脚本:
var url = 'https://script.google.com/macros/s/###/exec';
var settings = {
'url': url,
'method': 'GET',
'dataType':"json",
'data': {spreadsheetId: "###SpreadsheetId###", sheetName: "###SheetName###"},
};
$.ajax(settings).done(res => {
console.log(res.values); // By this, you can retrieve the values of `{values: values}` from Web Apps.
}).fail(err => console.log(err));
注:
- 当您修改了 Web 应用程序的 Google Apps 脚本时,请将 Web 应用程序重新部署为新版本。由此,最新的脚本被反映到Web Apps。请注意这一点。
- 在上面修改后的脚本中,可以使用您的 Google Apps 脚本。
- 在这种情况下,我可以确认上述脚本适用于 Chrome。