Site icon codippa

How to send ajax request from jsp to server using jQuery

Ajax Request…What?

Ajax stands for Asynchronous Javascript and XML. It is a client side technology which is used for creating dynamic web pages. Its main use is in scenarios where you want to send some data from the browser to the server and / or fetch data from the server Without Page Refresh. That’s right…without page refresh.

With ajax you don’t need to load the page again and again to show the refreshed data coming from the server. It is also used when you need to refresh a particular area of your web page. Refresh here means changing the content of a particular area.
For Example, on Facebook page you might have seen Notifications changing automatically on the side bar while you are on the same page. That is ajax.

Sending an ajax request from jsp to a server url involves the following code:

Let’s say we have a button on whose click we have to send the request. On this button’s click we call a function called sendRequest:

function sendRequest(){
//jQuery syntax for sending ajax request
$.ajax({
//options follow
url:'receiveRequest', //the url at which you want to send request
data:'id='+1 //the request parameters which are to be sent to server.
type:'Post', //type of request. Possible values are Get, Post, Put, Delete
success: function(){ //callback function to be invoked when the request is successfully returns
alert(“Request Successful”);
});

Details :
ajax call is initiated by jQuery’s ajax() method. There are various options available in this method but required ones have been used in the above code sample. Brief explanation of those options follows :

 

Let’s tweak in :
  1. Ajax call is always written in javascript and so it is invoked from client side.
  2. A request is considered successful when it returns a response code of 200.
  3. If the url specified in the request does not exist on the server, then a response code of 404 is returned.
  4. If the server method returns a response back to jsp (or any other view) then it may be easily retrieved in the success callback method. You need to add an argument in the method signature as:
    <>success: function(response){}
  5. The response argument in the success callback method contains the response sent by the server.
  6. There are several options available in jQuery for ajax request such as complete, async, timeout etc. For complete list of options and other details, check this out.
  7. If the server is using Spring controller then the controller method for above request would be:
  // Specifies the url and the method type. They should match the request
  //options on jsp else this would never be called
  @RequestMapping(value=“receiveRequest”, method=RequestMethod.POST)
  // Request from the browser sends a parameter given in the 'data' option of the ajax request
  public void receiver(@RequestParam("id") String id){}

Was this post helpful?? Great!!! Make it helpful for others too. Share it…

Exit mobile version