I'm using Google App开发者_StackOverflow中文版 Engine SDK for Python. When I write Javascript functions in a separate .js file and include it in the .py file, I get the following errors in Chrome:-
In .py file:-
Uncaught reference error: initFunc is not defined.In .js file:-
Uncaught syntax error: Unexpected token < Resource interpreted as script but transferred with MIME type text/html.Source codes:-
.py fileprint 'Content-Type: text/html'
print ''
print '\
<head>\
<title>Page</title>\
<script type="text/javascript" src="script.js">\
</script>\
</head>\
<body>\
<input type="button" onclick="initFunc();" value="Test" />\
</body>\
'
.js file
function initFunc(){alert("hi");}\
All the errors disappear when I include the initFunc in the .py file itself.
My guess is that you have both main.py
and foo.js
in the same root directory.
You should map a route for the static contents of your project like javascripts in the app.yaml
config file:
1.Create a directory called static/javascripts
in the Root of your project
2.Add the javascripts static section in your app.yaml
handlers
-url: /javascripts
static_dir: app/static/javascripts
3.Modify your code like this:
print 'Content-Type: text/html'
print ''
print """
<head>
<title>Page</title>
<script type="text/javascript" src="/javascripts/script.js">
</script>
</head>
<body>
<input type="button" onclick="initFunc();" value="Test" />
</body>
"""
As Wooble correctly suggested, you should avoid to code with print
and switch to a more fancy Python web-frameworks supported by GAE.
精彩评论