抖阴社区

Chapter 7: Earning lots of Money

10 0 1
                                    


Jeff downloaded the Zip file, extracting its contents, and then opened the project folder. Inside were typical web development files.

Like index.html, style.css, script.js, and a README.txt, along with a screenshot showing a browser console error.

He then opened Visual Studio Code, navigated to the project directory, and began the setup.
...
Bash

npm install
npm start
...

The app loaded in the browser. It was a simple contact form with fields for Name, Email, and Message. The design was clean, but there were a few noticeable bugs.

Jeff filled in a test message and clicked Submit.

Click!

The page refreshed after waiting for a while for the confirmation. No message was sent to the server.

He opened the browser's DevTools console, and sure enough, an error glared back. Making him glare as well.

[Uncaught TypeError: Cannot read properties of undefined (reading 'value')]

Jeff squinted at the screen, his eyes narrowing on line 27 of script.js. The code was trying to access form elements before the DOM had fully loaded.

He clicked over to the JavaScript file to confirm. Sure enough, the event listener had been declared outside the DOMContentLoaded block, which meant the script was trying to interact with elements that weren't available yet.

"Beginner's trap," he muttered to himself.

With his god-tier programming skill, he was literally not a beginner at this moment.

This was an easy fix for someone like him, a god of programming. Without wasting time, he got to work.

He wrapped the existing code inside a document.addEventListener('DOMContentLoaded', ...) block to ensure the script only ran after the entire page had finished loading.

Next, he noticed another common mistake. The form was missing event.preventDefault(), which meant the browser was refreshing the page before any JavaScript could execute properly.

"This is why your message isn't submitting, my guy," he mused inwardly, already rewriting the function with calm look.

He then added the following line.

...
JavaScript

form.addEventListener('submit', function(e) {
e.preventDefault();
// ...rest of code
});
...

(Authors Note: JavaScript is a scripting or programming language that enables you to create dynamically updating content, control multimedia, animate images, and much more on web pages. It is one of the core technologies of the web, alongside HTML and CSS)

Lastly, the fetch() function was present, but it lacked proper headers and didn't include JSON.stringify().

This meant that even if the request reached the server, it would likely cause an error or be rejected entirely.

So, he rewrote the submission logic properly. He added the necessary headers to specify the content type as JSON and wrapped the form data in JSON.stringify() to ensure it was correctly formatted before being sent to the server.

With a few precise keystrokes, the broken logic was replaced with clean, functional code.

...
JavaScript

fetch('/contact', {
method: 'POST',
headers: {...rest of code
...

"There was no validation, no error handling. this guy must be new in programming," Jeff thought as he saved the file.

You've reached the end of published parts.

? Last updated: Apr 04 ?

Add this story to your Library to get notified about new parts!

Lucky Spin: Godly ProgrammingWhere stories live. Discover now