내 Google Apps Script에서 AJAX 요청 리소스를 차단하는 'MIME 유형 불일치 오류'를 해결하려면 어떻게해야합니까?

K1 네 감각

나는 코드를 구현하기 위해 노력하고 여기를 함께 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에서도 비슷한 오류가 발생합니다. 어떻게 해결할 수 있습니까?

Tanaike

나는 당신의 목표를 다음과 같이 믿습니다.

  • 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));

노트 :

  • Web Apps의 Google Apps Script를 수정 한 경우 Web Apps를 새 버전으로 다시 배포하십시오. 이를 통해 최신 스크립트가 웹 앱에 반영됩니다. 조심하세요.
  • 위의 수정 된 스크립트에서 Google Apps 스크립트를 사용할 수 있습니다.
  • 이 경우 위의 스크립트가 Chrome에서 작동하는지 확인할 수 있습니다.

참고:

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관