I have a spreadsheet with a collection of cards, and I wrote a doGet()
function to pick a random one, and return it to the user. When entering the Web App url into a web browser, the result appears fine. However, when I try to make a HTTP Get request through the request module, it returns this:
<!DOCTYPE html><html><head><link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico"><title>Error</title><style type="text/css">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top: 25px;}</style></head><body style="margin:20px"><div><img alt="Google Apps Script" src="//ssl.gstatic.com/docs/script/images/logo.png"></div><div style="text-align:center;font-family:monospace;margin:50px auto 0;max-width:600px">We're sorry, a server error occurred while reading from storage. Error code INTERNAL.</div></body></html>
Extracted error message is:
We're sorry, a server error occurred while reading from storage. Error code INTERNAL.
I googled this error code, but nobody's had the same ending of "Error code INTERNAL", and most were about other users. Here is my doGet function and request function
Google Apps Script:
function doGet(request){
var requestType = request.parameter.type
if(requestType == "random_card"){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var entriesL = sheet.getLastRow() - 1;
var cardNum = getRandomInt(entriesL);
var card = SpreadsheetApp.getActiveSheet().getRange(2 + cardNum, 2).getValue();
var set = SpreadsheetApp.getActiveSheet().getRange(2 + cardNum, 1).getValue();
var output = '{"card":"' + card + '","cardSet":"' + set + '"}';
Logger.log(output)
return ContentService.createTextOutput(output);
}
}
Node.js
function randomCard(){
var info;
request.get('{url here}?action=get&type=random_card',function(err,res,body){
console.log(body)
info = JSON.parse(body);
});
return info;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…