나는 코드를 구현하기 위해 노력하고 여기를 함께 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 Script는 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()
대신을 사용하여 Web Apps에 요청하려고합니다 fetch
.이를 위해 귀하 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));
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다