Happy Coding

AJAX or AJAJ when to use JSON or always use it?


#1

Hello
thank you for teaching

I have read the AJAX and JSON lesson

Gently : would you use JSON “always” or are there situations where other manners are preferable?

e.g. forms
e.g. just variables
e.g. how to handle AJAX / AJAJ tables with pagination and filters and sorting (I don’t mean re- code datatables from scratch but it is a good example)
or it is matter of the rsponse

e.g. receive variables from the controller or receive html for some of the DOM elements

thank you in advance if you’ll expand your lesson with this (eventual) distinctions or confirmation (use always json) or what ever

Robert
Verona
Italy


#2

Like most things in programming, AJAX and JSON are both tools. Sometimes they’re the right choice, but other times they aren’t. It’s hard to be more specific than that, because so much depends on the context of whatever you’re working on.

So sure, sometimes it might make more sense to use form data rather than encoding your data as JSON. Or it might make sense to return a simple value or text value. It all depends.

The thing that makes JSON so versatile is that it can be used pretty much automatically in JavaScript, without a ton of additional parsing. But that doesn’t means it’s always the right tool for the job.


#3

Thank you for your kind reply

I guessed something like that

But as teacher , don’t you want to kindly give some guidelines for the best approach with some common situations? :slight_smile:

Ok, what common situations are

  • forms
  • tables of values used as form (e.g. columns and rows of an inventory)
  • multiple fields of an input page
  • I’m sure you’ll have a couple more
  • e.g. the way you load and submit this reply form when you click the edit icon :slight_smile:

yes the argument is wide

but some hints for those that in your opinion are common tasks… well it would be welcome.

Yes in the end coding is something subjective , and with practice you’ll recognize or stick with the methid that is good for you for that application

… but in the beginning, some inputs and hints are welcome, call them suggestions

P.S. what about the more recent fetch() ? With Google and Microsoft that are controlling the world (at least at browsers level) today as today there are almost no more outdated browser, since they obligate you to update it or you find it updated.

Thank you