WHAT'S NEW?
Loading...

More advanced implementation of AJAX in Web Projects

AJAX provides faster performance and dynamic user experience.

Working on many web projects at both front end and back end, implementing AJAX for communicating with the server about the user interactions in the browser at client side is always something I used to imagine in my applications when I was fresh at web development. I had been working with $.AJAX() function to do such AJAX posting of data of forms and other message passing between server and client. But I never returned an object from server to client so that i could convert the response from server directly to something like objects in javascript and prevent the parsing of response from the server.

Being specific, the response I am talking is the JSON response from server that is the result of json_encode() function in PHP. We do some task of retrieving the data as per the request from client to server, then collect them in a PHP array and finally json_encode($array). The result of this is a JSON response.

WHY JSON over traditional XML for cross-platform message passing?
XML Parsing is so much tough and takes heavy cost in terms of executing the parser. There are open tags and close tags for objects and properties of objects. Moreover, there are attributes that increase the complexity and time in parsing. But JSON is simple to parse. JSON requires less storage than XML for same message representation.

This is to my knowledge.

But I also experienced another usability of JSON response. To those platforms where data size has to be reduced for communication, JSON is the best approach. Exactly, I am talking about the mobile platform where size of data to be communicated should be as much less as possible.

Another thing of concern is, JSON Response from an API. A scenario where we need to built a web service or we need to develop some multiple software products from the use of a single database. The database should be accessible from web, mobile apps and so on. In such scenario, there can re-usability of same API functions for all software products of a Business Concern.

HENCE JSON OVER XML!

To the point!
Now, lets get more advanced to the functionality I am providing currently in my applications: "get some data from server as JSON response and dynamically generate html elements to provide full dynamic user experience to the response user wants". Get the JSON response and use javascript eval() function to generate the object representation of the JSON response. No bother of parsing the response to extract data from response.

The use is as:
Server side script in PHP:
<?php
/*here are some operations to get data from server like price, qty, color of a product the user wants to view. Instead a simple assignment is shown here.*/

$price = 50;
$qty = 100;
$color = 'white';

$arr = array('price' => $price,
                      'qty' => $qty,
                      'color' => $color
            );
echo json_encode($arr);
?>

The response from server of this script is:
{"price":50,"qty":100,"color":"white"}

Now,
After AJAX posting, I evaluate the response as:
var product = eval("("+reponse_text+")");

Here response_text is the response from the server.
So, by this time I can access the data items of object product as: product.price, product.qty and product.color in javascript.

ALL I WANT FOR MAKING USER EXPERIENCE BETTER IN SIMPLE WAY!

0 comments:

Post a Comment