如何解决"MIME类型不匹配错误",该问题阻止了我的Google Apps脚本中AJAX请求的资源? [英] How do I resolve a 'MIME type mismatch error' blocking an AJAX-requested resource from my Google Apps Script?
问题描述
我正在尝试使用 Jquery.ajax
而不是在此处实施代码>获取
.
I am trying to implement the code here with Jquery.ajax
rather than fetch
.
调用AJAX时出现以下错误:
I get the following error when I make the AJAX call:
由于MIME类型("application/json")不匹配(X-Content-Type-Options),"https://script.googleusercontent.com/macros/echo?..."中的资源已被阻止:nosniff).
Google Apps脚本与cURL可以很好地配合使用,因此我怀疑我的(非常简单的)GApps脚本和客户端脚本之间存在一些分歧.
The Google Apps Script works fine with cURL, so I suspect it's some disagreement between my (very simple) GApps script and my client script.
这是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
,但没有成功.
Note: I have tried changing the MimeType
on the last line to JSON
with no success.
这是我在客户端脚本中为呼叫设置的AJAX:
Here are my AJAX settings for the call in the client script:
var settings = {
'cache': false,
'dataType': "jsonp",
'async': true,
'crossDomain': true,
'url': url,
'method': 'GET',
'headers': {
'accept': 'application/json',
'Access-Control-Allow-Origin': '*'
}
};
我正在Firefox中工作.Chrome会引发类似的错误.我该如何解决?
I'm working in Firefox. A similar error is thrown in Chrome. How can I resolve this?
推荐答案
我相信您的目标如下.
- 您要使用
jQuery.ajax()
而不是fetch
来请求Web Apps.
- You want to request to the Web Apps using
jQuery.ajax()
instead offetch
.
为此,当您修改了设置
时,如何进行以下修改?
For this, when your settings
is modified, how about the following modification?
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 Apps的Google Apps脚本时,请重新部署Web Apps作为新版本.这样,最新脚本将反映到Web应用程序中.请注意这一点.
- 在上面修改的脚本中,可以使用您的Google Apps脚本.
- 在这种情况下,我可以确认上述脚本可用于Chrome.
这篇关于如何解决"MIME类型不匹配错误",该问题阻止了我的Google Apps脚本中AJAX请求的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!