Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
Which way for ajax response is better?

I thinking how I can design response to frontend part of website.
I want to know which solution and why is better ?

  1. Response as html
  2. Response as json

To back-end side I using PHP with template engine TWIG. To front-end side I using jQuery. To front-end I don't use any template engine.

I won't use template engine for back-end and front-end side, bacause I will have to duplicate template . It seems for me bad solution. Or maybe should I do it in this way ? maybe exist template engine which are able to use it in both case ?

Please could someone me advise ?

English is not my native language, so sorry if it hard to understand. I believe someone will know what I mean :)

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

JSON will be "better" in the vast majority of cases.

  • The information transferred from the server will be smaller. Consider: ["john","clara","paul"] vs <ul><li>john</li><li>clara</li><li>paul</li></ul>

  • Your back-end will be more flexible in how it can be used, you will be able to update your front-end markup independently of the back-end. JSON is also easier to manipulate with JavaScript than HTML.

The only exception to this is if you want to hide the logic used to generate the html from the public, but even in that case I'd favor sending JSON for the flexibility advantage.

over 4 years ago · Santiago Trujillo Report

0

There's no better solution. I myself prefer all my ajax calls to return JSON because I can just let the front end determine what to do with it. This way I'm maintaining separation of concerns. But like I said, it's a personal preference. Outputting HTML isn't considered bad practice per say.

I'm not sure what you mean by back end templates and front end templates though.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!