javascript - unable to read respone from python server -


i writing simple python server , using do_get return html following

from basehttpserver import httpserver, basehttprequesthandler  class requesthandler(basehttprequesthandler):    def _writeheaders(self):        self.send_response(200)        self.send_header('content-type', 'text/html')        self.end_headers()     def do_head(self):        self._writeheaders()     def do_get(self):        f = open("/full/path/to.html")        self._writeheaders()        self.wfile.write(f.read())   serveraddr = ('localhost', 7070) srvr = httpserver(serveraddr, requesthandler) srvr.serve_forever() 

in html have

<html> <head>     <title>mychart</title>     <meta charset="utf-8"> </head>  <div >     ...divs... </div>   <script>  ...js functions...  </script>  <body>  <script src="js/jquery-1.8.2.min.js" type="text/javascript"></script>   </body> </html> 

i able html, in browser console says uncaught syntaxerror: unexpected token < @ jquery-1.8.2.min.js:1

if directly open html page in browser, fine problem not in html

----update----

i using chrome , clicking error in console, source showed html instead of js file. tried specify full path of js file in html, still showed me html file in error

there js file works fine when open html file enter dir in browser

also in python console can see:

127.0.0.1 - - [28/jun/2015 00:12:28] "get / http/1.1" 200 - 127.0.0.1 - - [28/jun/2015 00:12:28] "get /full/path/to/js/jquery-1.8.2.min.js http/1.1" 200 - 127.0.0.1 - - [28/jun/2015 00:12:28] "get /favicon.ico http/1.1" 200 - 

your requesthandler serve html page regardless of url—whether url /, /index.html, /favicon.ico, or (in case) /js/jquery-1.8.2.min.js. if want /js/jquery-1.8.2.min.js serve actual javascript file rather html page again, you’ll need handle specially, e.g.:

def do_get(self):     if self.path == '/js/jquery-1.8.2.min.js':         filename = 'js/jquery-1.8.2.min.js'     else:         filename = 'index.html'     open(filename, 'r') f:         self._writeheaders()         self.wfile.write(f.read()) 

you’ll want change little bit such requesting javascript file gives appropriate content-type: text/javascript header rather content-type: text/html, , head returns headers consistent get, etc.

also, if ever begin start serving large files, may want consider using shutil.copyfileobj copy data file self.wfile, rather reading whole file memory , writing out.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -