Using EthernetServer Browser doesn't interpretate whole Javascript

Hello everyone, I am currently trying to get a webserver running on my Mega2560.
The server itself works quite fine, but over the course of handling multiple GET requests it fails.

When opening the Webpage the server recieves a GET request for the '.../' directory on which the server sends a response by sending a html file.
In such file there is script Tag refering to the app.js file on which the server receives the next GET request asking for that file. Until here more or less everything is stable and works just fine.
But when sending the contents of named .js file only the functions are visible on the page.

This means, that all global variables, that may be used inside some functions, will not be defined inside the javascript on the browsers side.

I have written a test szenario which functions exactly like the problem with my actual files:

The html file:

<!DOCTYPE html>
<html lang="de">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link id="favicon" rel="icon" type="image/png" href="Favicons/MinhRP2.jpg" sizes="32x32">
        <title>Get some Animals</title>

        <style>
            h1{
                text-align: center;
            }

            .centered-button{
                    margin:auto;
                    display:block;
            }
        </style>
    </head>
    
    <body>
        <h1>Testpage</h1>
        <div class="button-section">
            <button onclick="openAlert()">Open Alert</button>
        </div>
        <div class="button-section">
            <button onclick="openCrash()">Open Crash</button>
        </div>

        
        <script src="testCode.js">

        </script>
    </body>
</html>

The Javascript code that the server writes as response:

// This code will not be shown when send from Arduino Mega2560
var unseenVar = 'I will not be seen on the browsers source code :3'

function openAlert(){
    window.alert("This function will be uploaded onto the browser")
}

function openCrash(){
    // This will throw an error, due to the 'unseenVar' not being defined
    window.alert("But this variable will not: " + unseenVar)
}

Javascript code visible from the source view of the browser:

function openAlert(){
    window.alert("This function will be uploaded onto the browser")
}

function openCrash(){
    // This will throw an error, due to the 'unseenVar' not being defined
    window.alert("But this variable will not: " + unseenVar)
}

Error message inside the console of the browser when pressing the 'Open Crash' button:
Uncaught ReferenceError: unseenVar is not defined

Last but not least the code which sends each response from server site:

void sendFileContent(EthernetClient *client, String* filename){
  File file = SD.open(*filename);

  // Send confirmation
  client->println("HTTP/1.1 200 OK");
  
  if(file){
    // Send the Content-Type the file has to be interpreted in
    client->println("Content-Type: " + getContentType(filename));

    // Send every single character (due to dynamic storage shortage)
    while(file.available()){
    
      if(!client->write(static_cast<char>(file.read()))){
        Serial.println("Dropped something there!!!!!!!!");
      }
    }
  }
  else{
    client->print("404 File was not able to load gomenasaiiiiii");
    Serial.println("IOException: could not open " + *filename + "!!!!");
  }
}

String getContentType(String *filename){
  if(filename->endsWith(".htm")){
    return "text/html";
  }
  else if(filename->endsWith(".js")){
    return "text/javascript";
  }
  else if(filename->endsWith(".jpg")){
    return "image/jpg";
  }
  else if(filename->endsWith(".png")){
    return "image/png";
  }
  else{
    return "";
  }
}

/*
* After sending the File a short delay happens, and client.close() will be called
*/

I am fairly new to the HTTP-Webserver subject on Arduino (have some little experience with node.js). Therefore I have absolutally no Idea where I could have made a mistake on telling the Browser which filecontent he has to interpretate in which way.

But in my head, it makes no sence that any part of the javascript file gets treated differently from other parts ( here the functions) due to the fact that I have clearly told that following file will be written in text and has to be interpreted as javascript code.

try to add Content-length header

my WebServer IsgModbusTcpSG/WebServer.ino at master · JAndrassy/IsgModbusTcpSG · GitHub

The server doesn't tell that to the browser. But your code does some thinks wrongly. If you reply with HTTP/1.1 you should comply to that version of the protocol. You don't provide the content-size header but I'm almost sure that isn't the problem you have.

I see in your code that you use the String class, this should be avoided on AVR Arduinos (memory fragmentation). Maybe there are other problems in the rest of the code you're hiding from us.

Did you try to download the file using the command line? Rather suspicious is that the line the browser doesn't display is at the beginning of the file.

I don't quite understand. What do you mean by downloading and which command line do U referr to?
All I can say is, that opening the html file on my PC restults in so errors, meaning that the file gets loaded differently when opened from the PC, in comparison to writing the file as response from the webserver.

That is, why I actually made this post. I guess that there is something wrong with some sort of protocolls which, I suppose, I don't follow correctly.

But because the missing code is at the beginning, and therefore the first thing the client should read, I think that:

... wont actually fix my problem.

By the way, thanks for the quick replies and suggestions and I try to follow and respect all of them.

I will be working on the response header now and see how it changes things :slight_smile:

.....
I have read some more articles and whatched really closely

As shown on the above illustration of an HTTP response (and more), there is supposed to be an empty line finishing the header.
A lot makes sence now: Apperantly the client received my HTTP header and immediately got the global variabels from the first lines of the javascript code.
But because I don't make endlines in bewteen or above mentioned variables, the client interpretates them as part of the header that just could not be resolved to anything....

So after adding this little small endline AND including the Content-length parameter within the header everything is functioning just fine :slight_smile:

Thanks again for all the suggestions they really helped back on the track and got me figuring out what exactly it is that I am searching for, so I can google it more efficiently :+1:

The browser does a lot of things in the background that you cannot see without the developer tools. I meant downloading the file using a tool like curl (that exists for all major operating systems). If you want more control you can use the basic telnet command but then you have to do HTTP yourself.

I guess your browser does something wrong here but debugging that without actually seeing what's going on is very difficult.

You don't know that before having tried it. You never know what your browser does if the protocol is not followed correctly.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.