News for hackers. Stuff that matters.
Re:Browsers, JSON, and FormData by TheRealPomax on Saturday November 12, 2022 @12:17PM (#33575019)
This really strikes me as a shining example of "porque no los dos":

  "So many third-party services have APIs you can integrate with and guess what? They probably speak JSON exclusively. But browsers don’t send user-entered data as JSON by default, so what’s one to do? You can:
  
  "1. Create a URL that accepts the structure of a default browser POST request (<form method="post" action="/my-url">).
  
  "2. Throw progressive enhancement out the door, expect JavaScript to work for all your users, and drop a <script> tag on the page somewhere that handles the logic to submit a JSON payload to whatever service will store this information for you."
No, you do both: you create a server route that accepts that form's POST (or PUT or DELETE or whatever) and you write the code necessary to intercept the form's submit event and runs that same post operation backed by a Fetch request instead, then work the result into the page without needing the navigation action that a bare form uses. Congrats: your site now works both with limited-to-no JS, and as progressive web page when JS is fully available. All you had to do was stick with the basics: have a restful server.

There are no comments on this comment yet.