简体   繁体   中英

How do I link an external js file that returns code 302

I am trying to combine Github Pages with Google Apps Script so I can have Server Side Scripting with Github Pages. I try to connect to the Google Script web app using:

<script src="https://script.google.com/macros/s/NO_LINK_FOR_YOU/dev">
    </script>

(I need that /dev there, google script says nothing was returned when I don't use it.)

That is supposed to (and does) return:

return ContentService.createTextOutput("window.onload = function(){document.getElementById(\"request\").innerHTML = \"Generated: " + generateRandomNumber(10, 42) + "\";}");

Which outputs this:

window.onload = function(){document.getElementById("request").innerHTML = "Generated: 28";}

(Of course, it would not always be 28.)

When I load this into the browser, it does nothing. I looked in inspect element and it says that it's returning the code 302 (Temporarily Moved). This is usually used for redirects, and content service always makes the browser redirect "for security reasons", so this is expected.

But how can I get the browser to follow that redirect and get the script from there? Can I even do that?

In this case, a mimetype error occurs, since mimetype is not set. So please add setMimeType() as follows.

return ContentService
        .createTextOutput("window.onload = function(){document.getElementById(\"request\").innerHTML = \"Generated: " + generateRandomNumber(10, 42) + "\";}")
        .setMimeType(ContentService.MimeType.JAVASCRIPT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM