Saturday, 3 December 2016

AJAX Basics

AJAX is Asynchronous JavaScript and Xml

AJAX allows you to send and receive data asynchronously without reloading the web page. So it is fast.
           AJAX allows you to send only important information to the server not the entire page.
So only valuable data from the client side is routed to the server side. It makes your application interactive and faster.

Why used (advantages)-
1.User is not blocked
2.Faster

What is XmlHttpResuest  ?
This Object is used to communicate between client and server.

Main Functions -
1. sends data from client in the background
2. receives data from server
3. updates webpage with new data without reloading it.


XmlHttpRequest Properties - 
readyState - State of request
                   0 - un-opened
                   1 - opened but send() not called
                   2 - send() called
                   3 - downloading data and responseText holds data
                   4 - request completed fully (response ready)

responseText -   returns response as Text
responseXML - returns response as XML

              
XmlHttpRequest Methods - 
1 .void open(method,URL,sync)  
                method - get or post  
                Url - url to be hit
                Sync - synchronous or asynchronous call

2. send(String) 
                 With String - post request
                 Without String - get request 

When is response received ?  
request.readyState == 4 

How to connect with DB ?
Use Scriplate Tag  <% %> to connect with Db and fire query.

link --> http://www.javatpoint.com/ajax-example-with-database
 

Why not to use(Disadvantage) -
1. makes jsp's complex by using a lot of script code.
    Eg.
   In case of database interaction.
2. difficult debugging
3. security issues  (AJAX source code is readable and also scripts can be changed)         
                

How to call AJAX using jQuery:
1.$.get(url,data,callback(result));
   $.post(url,data,callback(result));

2.$.ajax(url.data).done(callback(result))
 


No comments:

Post a Comment