我在前端应用程序下使用Python使用谷歌云平台。
我尝试从单独的文件运行 java 脚本函数,同时使用 Google App Engine Launcher 在本地计算机上进行调试。
我也使用Python。
我在同一文件夹中有四个文件:
app.yaml:
application: test
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: .*
script: main.app
- url: /.js
static_files: /test.js
upload: /test.js
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: latest
- name: markupsafe
version: latest
main.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HELLO</title>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<input type="button" name="Test" value="Test" onclick="testclick()"/>
</body>
</html>
main.py:
import jinja2
import os
import webapp2
template_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.getcwd()))
class MainHandler(webapp2.RequestHandler):
def get(self):
template = template_env.get_template('main.html')
self.response.out.write(template.render())
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
和test.js:
function testclick()
{
alert('module test.js');
}
我收到一个错误 获取http://localhost:8080/test.js net::ERR_ABORTED 404(未找到) 单击按钮时。
我应该如何修改 yaml 文件来解决这个问题?
最佳答案
app.yaml
文件中处理程序的顺序很重要,您的 /.js
条目永远不会被命中,因为它将与 匹配.*
模式首先,您需要颠倒它们的顺序。
所以,我编辑了您的 app.yaml
文件,它对我来说工作得很好。
application: test
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /test.js
static_files: test.js
upload: test.js
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: latest
- name: markupsafe
version: latest
关于javascript - gcp上的html中的js(本地测试)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326754/