user1909647
user1909647

Reputation:

How to make AJAX request against JSP servlet

I know Question is a little ambiguous,But i am unable to describe it in simpler language.

Problem: I want to get a chunk of data from database using ajax with jquery.I know how to get data from database and send it as response but Problem is how to form request in "ajax with jquery" and get the response.

After that I need to pass "what we get from Database on client-side" in a function(Javascript) which can do something depending on the response.

I will be using a jsp page to send request. Request from ajax will go to servlet, and again response will come to same jsp page.

Upvotes: 0

Views: 4659

Answers (4)

bipen
bipen

Reputation: 36531

client side ajax jquery call

 $.ajax({
      url: path/to/your/function,
      data: '',//data tobe  send to the server (optional)
      type:'post', //either post or get
      dataType: 'json', //get response as json fron server
      success:function(data){  //this function is called when the ajax function is successfully executed
            alert(data) ;  OR  console.log(data);
       }
  });

server side function..

make query to your data base... return your response as json...

 echo json_encode($result);   //example

Upvotes: 2

Stefan
Stefan

Reputation: 31

you can do this

$.ajax({
      url: url,
      data: '',
      dataType: 'json/xml', 
      success:function(res){  
          console.log(res);
       }
  });

Upvotes: 1

Ram
Ram

Reputation: 873

Javascript code is...

function ajaxProcessor(){
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {//FOR IE
    XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {//FOR ALL OTHER BROWSERS
    try {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        XMLHttpRequestObject = false;
    }
}
}
if (XMLHttpRequestObject) {

    XMLHttpRequestObject.open("POST", "YOUR URL OR ACTION FOR STRUTS USERS", true);

    XMLHttpRequestObject.setRequestHeader('Content-Type',
            'application/x-www-form-urlencoded');

}
XMLHttpRequestObject.onreadystatechange = function() {
    if (XMLHttpRequestObject.readyState == 4
            && XMLHttpRequestObject.status == 200) {

            y = XMLHttpRequestObject.responseText;

            //DO SOMETHING WITH RESPONSE HERE
        }

};
    //POSTING THE DATA 
XMLHttpRequestObject.send("VAR_NAME1=" + VALUE+ "&VAR_NAME2=" + VALUE);
}

Upvotes: 0

Ricardo Vieira
Ricardo Vieira

Reputation: 740

your question is very hard to understand, let see

if you want consume a service to get data like database or other with jquery you can see this - Consume Service Jquery AJAX

depending on the response you can do a condition to check if data is correct or not, or get the value fields or others, i dont know if this is what you need

Upvotes: 1

Related Questions