Message from JavaScript discussions

October 2020

— 😢


When you navigate your browser to an URL, it sends a GET request, asking for data. In most cases you will get back some HTML (check the source code output of your website)

— With fetch('some-other-url') you can dynamically trigger these requests

— Wow, worst explanation ever

— Knowing that the browser GETs is useless

— Ok. your take on explaining

— You can simply understand fetch as a way to ask your server information. You can fetch(url) and tell your server to do something based on the URLs. By default, getting a PHP file will (probably) call it and send the result to the browser.

Message permanent page

— Understanding HTTP is important though. Otherwise you will end up as php developer wondering why some requests both populate $_GET and $_POST.

Message permanent page

— So fetch("db.php?sql=select * from TABLE") will ask for "db.php" setting the ?sql somewhere in the PHP (probably the $_GET variable) and you'll have to process the input in the db.php file and echo something out.

Message permanent page

— Totally agree, but I am a firm believer in "first make it work, then understand why"

— I wonder how you explain telephones to your children 👀

— Also little complicated thing: fetch is async, so this means that it doesn't just return a value, it instead returns a Promise (that is, a wrapper for a value that can either contain it or tell you it is being processed.) To use it easily you'll have to prepend the call with await (that calls the promise and waits until it's done)

Message permanent page