Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you Ajax JSON or HTML? [closed]

Tags:

What is the preferred way to do AJAX.

If it was for a search page written in PHP using Jquery for the AJAX

How would you handle the response

a) Have the response contain all the relevant html/styling

or

b) Send pure JSON and have a javascript function build the html/styling around the javascript variables.

I can see advantages to both. 'a' is obviously easier whilst 'b' is more efficient (although gzip would probably make the difference negligible).

like image 947
Pablo Avatar asked Jul 28 '10 13:07

Pablo


People also ask

Is it better to transfer data AJAX or JSON?

JSON stores all the data in an array so data transfer makes easier. That's why JSON is the best for sharing data of any size even audio, video, etc. Its syntax is very easy to use.

Should I learn AJAX first or JSON?

Javascript is the language itself so you should learn it first. JSON is the javascript object notation so while you're learning Javascript you will be learning it too. Ajax is the API for async calls to the server so it has its own object which is consumed using JS.

Can AJAX be used with JSON?

According to the AJAX model, web applications can send and retrieve data from a server asynchronously without interfering with the display and the behavior of the existing page. Many developers use JSON to pass AJAX updates between the client and the server.

Is AJAX and JSON the same?

Despite that AJAX stands for Asynchronous JavaScript and XML, JSON is frequently used for sending and retrieving data from the server. JSON stands for JavaScript Object Notation. JSON is a data format that very close to a JavaScript object, except that it can't contain any functions or dynamic code.


1 Answers

I would go with the html:

  • doing the conversion from json to html will make the client slower (and you don't know anything about the performance of the user's client)
  • doing it in json means you have to convert twice (from your server's data structure to json, then from json to html)
  • you probably do rendering to html already on the server, so you have the infrastructure
  • network traffic difference will be negligable
  • you can use libraries that do all the work for you (e.g. with JQuery, an ajax call that returns html just becomes jQuery('#div').load(url)).
like image 78
Joeri Hendrickx Avatar answered Sep 17 '22 13:09

Joeri Hendrickx