2

I'm building a little server that sends a page with a script in javascript, it works when I try to open it in my browser, but if I request the page from the server, the page is recived, but not the script, so I get these errors:

Console output

Script.js is missing between the files:

Javascript file missing

Looks strange because from the network session i can see a request for the script with the status 200:

enter image description here

The js file i'm tryng to add is Chart.js, so I can't add it internally, it would become impossible to work with it, but for now the server is just a few lines of code in python that use the SimpleHTTPRequestHandler, and I'm probably going to replace it, may it be because the SimpleHTTPRequestHandler can't handle multiple file requests?

Here's the code, tried to make a snippet but it does't work there too (probably that's just me never wrote a snippet before):

HTML:

<!doctype html>
<html>
<head>
   <script src="script.js"></script>
</head>
<body>
   <p id = "paragraph"></p>
   <script>
      document.getElementById('paragraph').innerHTML = sayHello();
   </script>
</body>
</html>

JS:

function sayHello(){
   return "HelloWorld!"
}

Here is the python server script:

from http.server import HTTPServer, BaseHTTPRequestHandler


class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        with open("index.html", "r") as page:
            self.wfile.write(page.read())


httpd = HTTPServer(("192.168.1.100", 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()
Francesco
  • 31
  • 1
  • 8
  • Is it possible you accidentally put `` *inside* your script.js file? If so, remove them. –  Feb 08 '20 at 09:11
  • 1
    Btw, you can add `` to your `` to get rid of the character encoding error. And if you're looking for a nice Python web framework, try [flask](https://palletsprojects.com/p/flask/). –  Feb 08 '20 at 09:20
  • @ChrisG, no i din't, also this is just an example, the problem is for chart.js, and surely there are none there, anyway thanks for the suggestions – Francesco Feb 08 '20 at 09:38
  • 1
    I'm suspecting this because of the `expected expression, got '<' error` in line 1. You should add your python code to the question though because it looks like this question is mostly about that and less about JS. –  Feb 08 '20 at 09:58
  • 1
    Your python server only returns the HTML file. That is why your HTML loads but your JavaScript does not. When the JavaScript file is requested, the server returns the HTML file causing the `SyntaxError: expected expression, got '<'` error. – Daemon Beast Feb 08 '20 at 11:22
  • Did you notice my comment? – Daemon Beast Feb 08 '20 at 13:15
  • @Zera thanks, now the script is recived but as plain text, still don't understeand why I had a 200 on the script request even before but ok, now I have to figure out how to add the right mime type I guess... – Francesco Feb 08 '20 at 16:03
  • It was received with status code 200 because your python web server returns the status code 200: `self.send_response(200)`. – Daemon Beast Feb 08 '20 at 16:05

1 Answers1

-1

I think you get an element with id, tag name or class Name and add file

var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.onload = function() {
    callFunctionFromScript();
}
script.src = 'path/to/your-script.js';
head.appendChild(script);

also check this link

Ramin Rezaei
  • 170
  • 1
  • 3
  • 12
  • Thanks for the answer, but the problem is still there, also I'm not a guru of this platform but I think you should adapt your code to the request, no just copy it – Francesco Feb 08 '20 at 10:57
  • You can use a ` – Daemon Beast Feb 08 '20 at 11:25
  • @Zera do you mean something like ?, in case I've tried and it doesn't work, but as another user mentioned, this is probably a server issue – Francesco Feb 08 '20 at 13:04