JSON communication between server and client Part1
JSON (JavaScript Object Notation) provides lightweight data exchange between server and client. It is human readable.
Sending data in JSON format from server to client:
There are APIs present for many languages (Java, ASP, Flex, C, C++ etc). With the help of these APIs, we can get JSON representation for data or objects. This data can be send to client just like some other data.
On the client side browser can handle this format and one can retrieve the information send from server.
I am writing code sample for spring framework
public void getJSONInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
JSONObject result = new JSONObject();
try {
Object obj = new Object();
// To get JSON data from obj
JSONObject jsonObj = JSONObject.fromObject(obj);
result.put("result", "success");
result.put("jsonInfo", jsonObj);
} catch (Exception e) {
result.put("result", "error");
} finally {
// Write this jsonObj in HttpServletResponse
response.getWriter().print(result);
response.flushBuffer();
}
}
While on the client side, I am using jquery to make AJAX request,
function getJSONInfo() {
$.ajax({
url: '../location?q=getJSONInfo',
type: "POST",
cache: false,
dataType: "json",
success: function(response){
alert(response.jsonInfo);
},
error: function(msg){
}
});
}
As we can see from the example, an ajax request is made to get JSONInfo and result is displayed in popup window.
Sending data in JSON format from client to server will be covered in part2
Restoring Conventional Browser Navigation to AJAX Applications
This article was result of my experience in developing Rich Internet application. It is depiction of my fight against the pitfalls of AJAX application. User is happy if you give him a better usability and performance but he is not aware about the underline technologies. If all of the sudden the default browser navigation stops to work in the desired fashion, user will start to panic. The developer starts getting issues like my back button is not working, clicking refresh lost my work done, so and so forth. The first reaction of developer is this is AJAX world and it act this way only, what can I do? Then we have to look for the ways of supporting default browser navigation in AJAX world. There are few hacks for providing supporting for default browser’s navigation using javascript but AJAX provide more systematic way of doing this. In my article I will explain how to use DOJO history APIs to cope up with one of the pitfall of AJAX application.
To read the article please follow the link